GeeCON Prague 2014*
The moose likes Struts and the fly likes Struts2 (2.1.8.1) Checkbox/Radio button validaton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2 (2.1.8.1) Checkbox/Radio button validaton" Watch "Struts2 (2.1.8.1) Checkbox/Radio button validaton" New topic
Author

Struts2 (2.1.8.1) Checkbox/Radio button validaton

Patrick Kok
Ranch Hand

Joined: Nov 12, 2009
Posts: 42
Hi,
I cannot figure out the way to validate checkbox and radio button.
Am searching the internet, but nothing about validation for checkbox.
Since my checkbox id/value comes from a List, and the post values are in Set, I don't have an idea of how the validation.xml can check this.
I need to enforce the user must check at least one checkbox.
For radio button I can enforce a default value to prevent it from failure.

Any helps?
thanks
Patrick Kok
Ranch Hand

Joined: Nov 12, 2009
Posts: 42
okay, I solved the problem.
I found that it cannot be done using validation.xml.
Implementing the addFieldError() inside validate() of action will be working fine.
BTW, it seems struts2 official document doesn't mention very clearly the template on the presentation layer (OGNL).
Thanks anyway.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Of course it can be done using validation.xml, by using a field expression validator, if nothing else. (The most effective way would depend on how things are named, etc.)

There's a fair amount of OGNL documentation on the Struts 2 documentation wiki, and a link to the OGNL user manual--how much more documentation do you want? And that has nothing to do with templating--JSPs are the canonical JEE templating mechanism.
Patrick Kok
Ranch Hand

Joined: Nov 12, 2009
Posts: 42
David Newton wrote:Of course it can be done using validation.xml, by using a field expression validator, if nothing else. (The most effective way would depend on how things are named, etc.)

There's a fair amount of OGNL documentation on the Struts 2 documentation wiki, and a link to the OGNL user manual--how much more documentation do you want? And that has nothing to do with templating--JSPs are the canonical JEE templating mechanism.


My checkboxlist returns a model driven attribute of a Set.
This set is for my hibernate one-to-many operation.
validation.xml can do with single checkbox boolean or value check, but it didn't the case for me.
I am looking for a solution. ie. checkbox.size()? check on validation.xml.
From the official OGNL expression doc, I didn't see any examples of this.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Still don't know your usecase, so I can't really help. Expressions in the expression validator can be arbitrary OGNL, AFAIK, and OGNL can call arbitrary Java.

That said, for anything other than trivial validations, I almost always do it in Java code anyway, for clarity, code completion, type safety, and so on.
Patrick Kok
Ranch Hand

Joined: Nov 12, 2009
Posts: 42
David Newton wrote:Still don't know your usecase, so I can't really help. Expressions in the expression validator can be arbitrary OGNL, AFAIK, and OGNL can call arbitrary Java.

That said, for anything other than trivial validations, I almost always do it in Java code anyway, for clarity, code completion, type safety, and so on.


I had done some experiments on validation.
At last, I found that fieldexpression is what I want to use.

<field name="items">
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[items.size()>0]]></param>
<message key="items.blank.message"/>
</field-validator>
</field>


but this doesn't work!


<field name="items">
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[#items.size()>0]]></param>
<message key="items.blank.message"/>
</field-validator>
</field>


What is the purpose of this Shape(#) for?
The official doc:
http://struts.apache.org/2.1.8.1/docs/fieldexpression-validator.html
This really confuses me a lot!

And, I still don't accept the validation is not in intuitive logic.
I'd prefer the <param> requires a TRUE for triggering the message.
Though I can customize my own validator.


Patrick Kok
Ranch Hand

Joined: Nov 12, 2009
Posts: 42
David,
I do agree with you that putting business logic into model is good idea.
I am happy with the validate{$methodname}() for differentiating my actions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts2 (2.1.8.1) Checkbox/Radio button validaton