File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes How to check if field exist in validation.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to check if field exist in validation.xml " Watch "How to check if field exist in validation.xml " New topic
Author

How to check if field exist in validation.xml

Nina Anderson
Ranch Hand

Joined: Jul 18, 2006
Posts: 148
Hi guys,

I'm shared a form across 3 differenct jsp pages. Some of the jsp pages do not have all the fields in the form displayed. However, the validation is displaying error messages for the form fields that do not exist on the jsp page.

How do tell the validation.xml to only validate fields that are display on a page?

Here's my validation.xml:

<formset>
<form name="LoginForm">
<field property="userName" depends="required, email">
<msg name="email" key="errors.invalid" />
<arg0 key="prompt.username" />
</field>
<field property="passWord" depends="required, minlength, maxlength,mask">
<arg0 key="prompt.password" />
<arg1 key="${var:minlength}" name="minlength" resource="false" />
<arg2 key="${var:maxlength}" name="maxlength" resource="false" />
<arg3 key="${var:mask}" name="mask" resource="false" />
<var>
<var-name>minlength</var-name>
<var-value>8</var-value>
</var>
<var>
<var-name>maxlength</var-name>
<var-value>10</var-value>
</var>
<var>
<var-name>mask</var-name>
<var-value>(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,10})$</var-value>
</var>
</field>
</form>
</formset>
Craig Bayley
Ranch Hand

Joined: Sep 27, 2007
Posts: 46
Use a hidden field that stores what page you come from, then use that to filter the validation.

Alternatively, don't use validation on the form, call a validation method in your action class.

Either is a bit clumsy but will get the job done. There may be other more elegant methods, though.
Nina Anderson
Ranch Hand

Joined: Jul 18, 2006
Posts: 148
I need a more elegant solution.
Craig Bayley
Ranch Hand

Joined: Sep 27, 2007
Posts: 46
Then I'd suggest defining some custom validation rules.
Bhaskar Reddy
Ranch Hand

Joined: Dec 17, 2002
Posts: 52
I guess you may do something like this -
In your struts-config.xml file, have different form bean names for the same form - one each for each page.


Now, using this form bean name, you may have your validations declared in validation.xml file.
Craig Bayley
Ranch Hand

Joined: Sep 27, 2007
Posts: 46
I cringed initially, but on further reflection I like it. Very elegant.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to check if field exist in validation.xml
 
Similar Threads
problem in using mask validation
mask is not working
Validator for NOT NULL/ blanks
DynaValidatorActionForm not working
custom validator and custom tags