Struts 1.3 customized validwhen validator - validation

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);

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?

Freemarker Checkboxes input in Spring form

I have an HTML form bound to a Spring model to take in user data and add it to a database. This works fine. I have used Spring Freemarker macros for the fields to take input and validate before sending, e.g.
<#spring.formInput path="myForm.username"/>
<#spring.showErrors ", "/>
This also works fine for text input. What is causing me problems is rendering multiple checkboxes with the Spring macro. My original HTML was:
<input name="roleList" type="checkbox" value="1"/>Adminstrator
<input name="roleList" type="checkbox" value="2"/>Developer
<input name="roleList" type="checkbox" value="3"/>Customer
I created a Java Map<String, String> of this information with the keys as "1", "2", "3" in my controller method and added it to the model, then replaced the HTML with this macro in my ftl template:
<#spring.formCheckboxes path="quickForm.roleList" options="${roleMap}" separator="<br>"/>
But I get an error
Expecting a string, date or number here, Expression roleMap is instead a freemarker.template.SimpleHash
Why would it give that message if it requires a Map? (as in the Spring Docs for FreeMarker macros) Can anyone explain how I should be providing the checkbox data?
Finally worked this out, so for anyone who's interested: it wasn't as straightforward as just creating a HashMap of values and adding them with model.addAttribute("roleMap", myHash) - I had to create a service class to do this instead: it retrieved my list of roles from a database table and then converted them into a HashMap<String, String>. I then called this in my controller method, added it to my model (called "roleMap" here) and used it within my FreeMarker template without the usual formatting, like this:
<#spring.formCheckboxes path="quickForm.roleList" options=roleMap separator="<br>"/>
Having the data converted in a service method was key for Spring to use it as checkbox options.

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.

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"

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