Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to check if field exist in validation.xml

 
Nina Anderson
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need a more elegant solution.
 
Craig Bayley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then I'd suggest defining some custom validation rules.
 
Bhaskar Reddy
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cringed initially, but on further reflection I like it. Very elegant.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic