You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Validators can implement the com.opensymphony.xwork.validator.ScriptValidationAware interface to provide support for client-side validation:

public interface ScriptValidationAware extends FieldValidator {
    public String validationScript(Map parameters);
}

The value returned by validationScript will be executed on the client-side before the form is submitted if client-side validation is enabled. For example, the requiredstring validator has the following code:

public String validationScript(Map parameters) {
        String field = (String) parameters.get("name");
        StringBuffer js = new StringBuffer();

        js.append("value = form.elements['" + field + "'].value;\n");
        js.append("if (value == \"\") {\n");
        js.append("\talert('" + getMessage(null) + "');\n");
        js.append("\treturn '" + field + "';\n");
        js.append("}\n");
        js.append("\n");

        return js.toString();
}

To enable client-side validation, use the <ww:form> tag:

<ww:form name="'test'" action="'javascriptValidation'" validate="true" >

  ...
</ww:form>

Currently only JavaScript is supported.

  • No labels