Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

WebWork adds support for client-side validation on top of XWork's standard validation framework. You can enable it on a per-form basis by specifying validate="true" in the <ww:form> tag:

Code Block
html
html

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

  ...
</ww:form>

Upgrade Alert: To use client-side validation, make sure you have defined the correct validators in validators.xml. You must be using the com.opensymphony.webwork.validators.JavaScriptRequired*Validator version of the standard XWork validators.

Building a Validator that supports client-side validation

Any validator can be extended to support client-side validation by implementing the com.opensymphony.webwork.validators.ScriptValidationAware interface:

...

Code Block
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, first make sure you have defined the correct validators in validators.xml. You must be using the com.opensymphony.webwork.validators.JavaScriptRequired*Validator instead of the standard XWork validators. Then, when you specify the <ww:form> tag, add validate="true":

...


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

  ...
</ww:form>

Currently only JavaScript is supportedOnly JavaScript is supported at this time.