WebWork The framework adds support for client-side validation on top of XWork's the standard validation framework.
Client-side validation can be enabled You can enable it on a per-form basis by specifying validate="true"
in the <ww:form> tag:.
Code Block | ||||
---|---|---|---|---|
| ||||
<ww<s:form name="'test'" action="'javascriptValidation'" validate="true"> ... </wws:form> |
Of course, all the standard validation configuration steps still apply. Client-side validation uses the same validation rules as server-side validation.
Note that the "required" attribute on many WebWork UI tags has nothing to do with client-side validation.
...
If a name
for the form is not given, the action mapping name will be used as the form name. Otherwise, a correct action
and namespace
attributes must be provided to the <saf:form>
tag.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<s:form namespace="/user" action="submitProfile" validate="true">
...
</s:form>
|
Technically, the form's action attribute can refer to a "path" that includes the namespace and action as a URI. But, client-side validation requires that the action name and namespeact to be set separately.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<s:form action="/user/submitProfile.action" validate="true">
...
</s:form>
|
All the usual validation configuration steps apply to client-side validation. Client-side validation uses the same validation rules as server-side validation. If server-side validation doesn't work, then client-side validation won't work either.
Info | ||
---|---|---|
| ||
The |
Client Side Validation Types
There are two styles of client side validation.
Used by the xhtml theme and css_xhtml theme | |
Used by the ajax theme |
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 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:
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();
}
|
...