aspose file tools*
The moose likes Struts and the fly likes Conditional validation in Struts2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Conditional validation in Struts2" Watch "Conditional validation in Struts2" New topic
Author

Conditional validation in Struts2

Aneesh Anvar
Greenhorn

Joined: Oct 08, 2010
Posts: 6
I have a field in my application. but for some users that field will be disabled..
but if the field is displaying, it should be validated.. and no validation if it is disabled.
I'm using Struts2 and XML validation..

Can anyone help me in solving this issue?
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Post in the right forum next time. You posted in SCJP, but this belongs in the Struts forum

What have you done so far to achieve this requirement ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Aneesh Anvar
Greenhorn

Joined: Oct 08, 2010
Posts: 6
i've a field, which is validated client side in validation.xml..
But i want to make a generic validation.xml so that only when a particular field(say username) is present, it should validate, otherwise no need to validate..
But the code for the validation always remains there, for the field..If its present it ll validate, otherwise no... but in validation.xml, it wont support any condition like
(if username)
{
code for validate..
}

need to find out a new solution
Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

hi Aneesh..

are you using struts Validation framework..??
can you show your validation.xml?

Regards,
Raza..


Good luck!!
A small leak can sink a Gigantic ship.>
Aneesh Anvar
Greenhorn

Joined: Oct 08, 2010
Posts: 6
hi Raza,

i am using struts2 validators... Here goes my validation.xml. here the firstname should be given a condition, like only if firstname is present in the jsp page then the validation should happen, otherwise dont take the validation code... But the validation code for the firstname wll always be presernt in the validation.xml...
This is my requirement.. i think now you got a better understanding of the problem...

<validators>
<field name="firstName">
<field-validator type="requiredstring">
<message key="validator.requiredstring"/>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">1</param>
<param name="maxLength">30</param>
<message key="validator.stringlength"/>
</field-validator>
</field>
<validators>
Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

ok you can have a validate method in your Action class ..Rather than a Action-Name-Validation.xml.

In validate method you can perform the conditional validation.

For validate method your action class should implement ValidationAware Interface..

I think this might help you..If you have any problem then let me know.

Regards,
Raza.
Aneesh Anvar
Greenhorn

Joined: Oct 08, 2010
Posts: 6
hi raza,
thank you for the reply.. But the validate method will make it a server side validation. I need a perfect client side validation..
If i give it in validate method, then like that field( username), if any other field requires such kind of validation, then the code ll become messy...
If we ve any generic condtion in xml, thinhs ll be pretty easy
Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

Hi aneesh..

validation with xml file is also a server side validation.

But for Client side you need to add validate ='true ' on your Form.

You can populate value and then make it disable ...it will work with validation.xml.


Aneesh Anvar
Greenhorn

Joined: Oct 08, 2010
Posts: 6
hi raza,
i added the validate="true " in the form...The rest you wrote "You can populate value and then make it disable ...it will work with validation.xml. "
i couldnt make out. Coould you please explain much more clear...
Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

I mean validation.xml does not perform partial validation. Whatever is present inside validation.xml will be called for the validation.
Either you make the field value present by giving a default value or use validate method..

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Conditional validation in Struts2