problem in multiple validation.xml for single action in struts2 - validation

I am working on struts2 application.
I have a jsp page having 3 textfields. And I am validating each field through action-validation.xml file. Now I want if validation get fail at first field it should not check the other two fileds and result directly go to jsp page (say a.jsp) showing error message for that single filed only. And if validation does not fail at first field then it should check rest of the fileds, i.e second and third fileld and now if validation fails here then also result directly go to jsp page but different one (say b.jsp) showing error message.
Is it possible? If yes then please make me aware with this.
I have tried this but action-validation.xml is validating all fields in single shot and error messages for all fields is showing in a jsp page that I have written under a.jsp
Thanks in advance.

You can add a short-circuit in your field-validator
e.g.
<!-- Field Validators for email field -->
<field name="email">
<field-validator type="required" short-circuit="true">
<message>You must enter a value for email.</message>
</field-validator>
<field-validator type="email" short-circuit="true">
<message>Not a valid e-mail.</message>
</field-validator>
</field>
<!-- Field Validators for email2 field -->
<field name="email2">
<field-validator type="required">
<message>You must enter a value for email2.</message>
</field-validator>
<field-validator type="email">
<message>Not a valid e-mail2.</message>
</field-validator>
</field>
if email is empty or not valid, email2 will not be validated
http://struts.apache.org/2.x/docs/validation.html
"Failure of a particular validator marked as short-circuit will prevent the evaluation of subsequent validators"

Related

Distinguish between conversion failure and validation failure in o:viewParamValidationFailed

I am using the OmniFaces <o:viewParam> taghandler in my current project. I like it, it's great. And even greater is the <o:viewParamValidationFailed>. Now, we are able to send an error if validation or conversion fails. But I wonder, whether it is possible to distinguish between conversion failure and validation failure.
Let's say we want to send a Bad Request if the given view param in malformed and can not be converted; for that matter send a Not Found if conversion succeeded, but the object could not be found in the database; and send a Forbidden if the successfully fetched object should not be accessed by the user.
Does anybody know a way to achieve this?
It's unfortunately not possible to distinguish between a ConverterException and ValidatorException when you've only UIInput#isValid() at hands. Theoretically, you could check and test the faces message to see if it represents a conversion or validation error, but this is not a robust approach, certainly not when it's localized.
On the other hand, it's possible to declare multiple view parameters on the same parameter name. You do not necessarily need to specify a value to set it as model value.
Here's an example based on your description, note that the model value is only set on the last one:
<o:viewParam name="foo">
<f:converter converterId="yourFooConverter" />
<o:viewParamValidationFailed sendError="400" />
</o:viewParam>
<o:viewParam name="foo">
<f:converter converterId="yourFooConverter" />
<f:validateRequired />
<o:viewParamValidationFailed sendError="404" />
</o:viewParam>
<o:viewParam name="foo" value="#{bean.foo}">
<f:converter converterId="yourFooConverter" />
<f:validateRequired />
<f:validator validatorId="yourRestrictedAccessValidator" />
<o:viewParamValidationFailed sendError="403" />
</o:viewParam>
To avoid the expensive job of calling the DB on every conversion, let the YourFooConverter implementation store the converted value as a custom attribute of the FacesContext and then check it on every pass.

validatorMessage not used when non-number input is entered

I'd like to show a custom message when the user enters a non-number.
For that, I specified validatorMessage attribute:
<h:inputText value="#{bean.commission}" required="true"
requiredMessage="Please enter a value"
validatorMessage="Please enter a number" />
The required message appears correctly on empty input, but the validator message doesn't appear on non-number input. Instead, it shows the default message that the input value is not a number.
How is this caused and how can I solve it?
You faced a conversion error, not a validation error.
Use converterMessage instead.
<h:inputText value="#{bean.commission}" required="true"
requiredMessage="Please enter a value"
converterMessage="Please enter a number" />
The validatorMessage is only used on validators specified via validator attribute and those via nested <f:validator> and <f:validateXxx> tags.
The converterMessage is used on converters specified via converter attribute and those via nested <f:converter> and <f:convertXxx> tags, and also on auto-registered converters for a specific class, such as IntegerConverter in case of int/Integer typed value.
See also:
Accept only digits for h:inputText value
How validate number fields with validateRegex in a JSF-Page?

Struts 1.3 customized validwhen validator

I have a form in struts 1.3 which is dynamically generated one and having indexed properties.
I want to validate that form with struts validation framework.
Here is my situation:
there is a field called page which should be even number certain personnames. For this I have to create customize valid when in struts. Do u guys have any idea?
<field property="page" indexedListProperty="data"
depends="validwhenpage,integer,intRange">
<arg0 key="err.personname.page.valid"/>
<var>
<var-name>test</var-name>
<var-value>((data[].personname=='ramesh') or (*this* ????))</var-value>
</var>
</field>
we can create Action Error object and save the error message in our action class and we can do the validation what ever we want
errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("test","test1");
saveErrors(request, errors);

Struts 2 Clarification needed

I wonder how struts 2 validation is performed without specifying Validate = true in struts config xml. Can you anyone tell me the flow of Struts 2 validation using validation framework.
Validation happens through a combination of the "validation" and "workflow" interceptors. There is no "validate" setup in the Struts 2 config file, because it's unnecessary.
Struts core has the validation framework that assists the application to run the rules to perform validation before the action method is executed.
Actions class works as a domain data and it looks for the properties in its Action Mapping File and it searches the field validators in theFileName-Validation.xml and all validators work as per the field defined in validation.xml. If there is any mismatching of data, it picks the message from the validation.xml and displays it to user.
Sample Employee-validation.xml :
<validators>
<field name="name">
<field-validator type="required">
<message>
The name is required.
</message>
</field-validator>
</field>
<field name="age">
<field-validator type="int">
<param name="min">29</param>
<param name="max">64</param>
<message>
Age must be in between 28 and 65
</message>
</field-validator>
</field>
</validators>
This is the sample validation file for Employee model and request will be validated for properties name and age. If name field left empty validation will gives the error message as "The name is required" above the name input box And if the age entered is outside the limit of 29-64 validation will show an error as "Age must be in between 28 and 65" above the age input box.

struts2 validation

I have a text field and a selectbox (users can select multiple values by ctrl) in a form.
I was reading validation provided by struts2: http://struts.apache.org/2.x/docs/validation.html
However, it doesnt seem to have the validation I need.
I want to make the text field a required field ONLY when certain strings are selected from the selectbox.
Do I have to write a custom validator for this purpose or is there a simpler way to achieve this in struts2.
thanks!
PS: I would like to know how other languages/frameworks might handle this case also.
Expression or field expression caters for checking fields dependent on other fields. For example, to check for a confirmed email address you could do the following:
<field name="confirmAddress">
<field-validator type="fieldexpression">
<param name="expression">address == confirmAddress</param>
<message key="nomatch"/>
</field-validator>
</field>
Creating a custom validator is trivial though, and easy to reuse.

Resources