Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts2 (2.1.8.1) Checkbox/Radio button validaton

 
Patrick Kok
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic