short-circuiting and Validator flavors
Plain validator takes precedence over field-validator. They get validated first in the order they are defined and then the field-validator in the order they are defined. Failure of a particular validator marked as short-circuit will prevent the evaluation of subsequent validators and an error (action error or field error depending on the type of validator) will be added to the ValidationContext of the object being validated.
In the example above, the actual execution of validator would be as follows:
Since Plain Validator 2 is short-circuited, if its validation failed, it will causes Field validators for email field and Field validators for email2 field to not be validated as well.
Usefull Information: More complicated validation should probably be done in the validate() method on the action itself (assuming the action implements Validatable interface which ActionSupport already does).
A plain Validator (non FieldValidator) that gets short-circuited will completely break out of the validation stack. No other validators will be evaluated and plain validators takes precedence over field validators meaning that they get evaluated in the order they are defined before field validators get a chance to be evaluated.
Short cuircuiting and validator flavours
A FieldValidator that gets short-circuited will only prevent other FieldValidators for the same field from being evaluated. Note that this "same field" behavior applies regardless of whether the
<validator type="required" short-circuit="true"> <param name="fieldName">bar</param> <message>You must enter a value for bar.</message> </validator> <validator type="expression"> <param name="expression">foo gt bar</param> <message>foo must be great than bar.</message> </validator>
both validators will be run, even if the "required" validator short-circuits. "required" validators are FieldValidator's and will not short-circuit the plain ExpressionValidator because FieldValidators only short-circuit other checks on that same field. Since the plain Validator is not field specific, it is not short-circuited.
As mentioned above, the framework will also search up the inheritance tree of the action to find default validations for interfaces and parent classes of the Action. If you are using the short-circuit attribute and relying on default validators higher up in the inheritance tree, make sure you don't accidentally short-circuit things higher in the tree that you really want!
The effect of having common validators on both
It should be noted that the nett effect will be validation on both the validators available in both validation configuration file. For example if we have 'requiredstring' validators defined in both validation xml file for field named 'address', we will see 2 validation error indicating that the the address cannot be empty (assuming validation failed). This is due to WebWork will merge validators found in both validation configuration files.
The logic behind this design decision is such that we could have common validators in <actionClass>-validation.xml and more context specific validators to be located in <actionClass>-<actionAlias>-validation.xml
Validator's validation messages could be internatinalized. For example,<field-validator type="required"> <message key="required.field" /> </field-validator>or
<validator type="expression"> <param name="expression">email.startsWith('Mark')</param> <message key="email.invalid" /> </validator>In the first case, WebWork would look for i18n with key 'required.field' as the validation error message if validation fails, and 'email.invalid' in the second case. We could also provide a default message such that if validation failed and the i18n key for the message cannot be found, WebWork would fall back and use the default message. An example would be as follows :-
<field-validator type="required"> <message key="required.field">This field is required.</message> </field-validator>or
<validator type="expression"> <param name="expression">email.startsWith('Mark')</param> <message key="email.invalid">Email needs with starts with Mark</message> </validator>@author Jason Carreira
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|