struts2 validation - 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.

Related

OmniFaces validateOrder disabling

I'm trying to use validateOrder component to validate two java.util.Date objects. It is similar to showcase example on this link (PrimeFaces example). Everything works perfect, but i have one question:
What if 2nd date field is not required?
In that case i'm getting nullpointer exception, and since validateOrder has "disabled" attribute, i was wondering is it worth/possible enabling/disabling it via ajax every time the 2nd date is inserted/removed. If not, i guess i'll stick to Balus' approach for JSF2.0 cross-field validation that you can read about on this link.
Let the disabled attribute check if the 2nd field is filled in. If it's not filled in, the request parameter value associated with field's client ID will be empty. Use exaclty that to let disabled attribute evaluate to true.
<p:calendar ... binding="#{endDate}" />
...
<o:validateOrder ... disabled="#{empty param[endDate.clientId]}" />
Code is as-is. No additional backing bean property necessary for binding.
See also:
How does the 'binding' attribute work in JSF? When and how should it be used?

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.

Validating double field in struts 2

I have a field "length" in one of my struts 2 form. The data-type of "length" is "double". I have applied the "double" validation in XML file. But when I key-in alphabets in the "length" text field, it shows the error message as
Invalid field value for field "length"
I don't want this message to be shown like this. This message is generated by struts 2 itself and not entered by me. I guess, this message comes as the conversion of data fails. I also applied the "conversion" validator, but the above error message is still showing up. Please suggest the solution.
Thanks in advance.
You're in luck. This text is customizable.
The text is defined in xwork-messages.properties in the xwork jar. You can override it by adding the following to your global i18n resource bundle:
xwork.default.invalid.fieldvalue=Invalid field value for field "{0}".
As you guessed, this error message occurs for all type conversion failures. The XWorkConverter class has some useful javadoc about this:
Any error that occurs during type conversion may or may not wish to be reported. For example, reporting that the input "abc" could not be converted to a number might be important. On the other hand, reporting that an empty string, "", cannot be converted to a number might not be important - especially in a web environment where it is hard to distinguish between a user not entering a value vs. entering a blank value.
By default, all conversion errors are reported using the generic i18n key xwork.default.invalid.fieldvalue, which you can override (the default text is Invalid field value for field "xxx", where xxx is the field name) in your global i18n resource bundle.
However, sometimes you may wish to override this message on a per-field basis. You can do this by adding an i18n key associated with just your action (Action.properties) using the pattern invalid.fieldvalue.xxx, where xxx is the field name.
It is important to know that none of these errors are actually reported directly. Rather, they are added to a map called conversionErrors in the ActionContext. There are several ways this map can then be accessed and the errors can be reported accordingly.
try this,
<field name="percentage">
<field-validator type="double">
<param name="minExclusiveExpression">${minExclusiveValue}</param>
<param name="maxExclusiveExpression">${maxExclusiveValue}</param>
<message>Percentage needs to be between ${minExclusive} and ${maxExclusive} (exclusive)</message>
</field-validator>
</field>

problem in multiple validation.xml for single action in struts2

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"

Resources