Java Mailing List Archive

http://www.junlu.com/

Google
Google
Mailing List
Home
Forum Home
JBoss - Java Application Server
Tomcat - JSP/Servlet container
Struts - A MVC web framework
iText - An open source PDF Java Library
JDOM - JDOM XML Parser
JSP - A mailing list about Java Server Pages specification and reference
J2EE - A mailing list for Java(tm) 2 Platform, Enterprise Edition
J2EE Pattern - An interest list for Sun Java Center J2EE Pattern Catalog
Servlet - A mailing list for discussion about Sun Microsystem's Java Servlet API Technology
Struts & Hibernate
Subjects
JSP editor plugin for eclipse ?
org apache jasper JasperException: Unable to compile class for JSP
Tomcat: Connection reset by peer: socket write error
Cannot retrieve definition for form bean null
Struts Tiles Tutorial (free Struts training)
Where do I download Tomcat 4 0 6?
Data Access Object (DAO) pattern, example DAO 's
Where to download Tomcat v 4 1 24 from?
Tomcat 5 0 16 Requested resource not available
Subject: Servlet : Session invalidate
Oracle Connection Pooling in 3 2 2
Servlet action is currently unavailable
Tomcat/Struts Unicode Encoding/Decoding problems
Subject: Running a Simple JMS Example
Tomcat and webapplication specific java library path
Mapping in workers2 properties
org apache jasper JasperException
problem with html:text bean throwing exception
Cannot find message resources under key org apache struts action
   MESSAGE
Cannot find message resources under key org apache struts action MESSAGE
invalid direct reference problem with solution
Tool for jsp debug Try Sysdeo Eclipse Plugin
Tomcat 5 Cannot load JDBC driver class 'null ' SQL state: null
weblogic ejbc
java properties file
Jboss 3 2 3 Coyote Can 't re
Tomcat 5, Apache2 and mod jk2 integration problem
JBoss example problem new to J2EE
Value attribute of <html:checkbox
url string for connecting jboss to oracle
javax servlet ServletException: BeanUtils populate
5 0 18: Windows XP Pro vs Windows 2000
HTTP Status 404 The requested resource is not available
 
Conditional Validation

Conditional Validation

2007-11-04       - By Gary Affonso

 Back
naveen472-m@(protected) wrote:
> Can anyone tell me how to do conditional validation in struts 2?
>
> In one form I want validations for a particular category (say Category XYZ)
only if the candidate in that. If he is NOT Category XYZ then by checking a
checkbox he should be able to skip the validations for that. I am not able find
any solution to this problem. I guess there are no provisions for conditional
validation in Struts 2. Struts 1 had a <validwhen> tag. But I am not able to
find any equivalent tag for Struts 2.

This was true for WebWork, I'm assuming it remains true for s2...

If you're using the xml-based validation framework then you can use the
"expression" validator to do validations on as many fields/conditions
(and their combinations) as you want.

Here's an example:

    <validator type="expression">
        <param name="expression">
            ! (
                ( chosenAddressId.intValue() eq -1 )
                and
                ( regionId.intValue() eq -1 )
                and
                ( ( countryId.intValue() eq 1 ) or (
countryId.intValue() eq 2 ) )
               )
        </param>
        <message>
If you choose to use a custom address and choose US or Canada as your
country, you must choose a state/region from the drop-down menu.</message>
    </validator>

The HTML that the above code validates has three fields.  A radio button
with a "custom" address choice, a country drop-down (select) and a
state/region drop-down (the drop-downs have IDs for values).

The above validation says that a region choice is required if the user
chooses the "custom address" radio button AND they choose US or Canada
for the country drop-down.

Be warned, though.  Writing and debugging complex OGNL validation
expressions absolutely sucks (IMO).  It's confusing to get the logic
right (you'll notice that I've written the checks above as positive
statements and then reversed the whole expression.  I find writing the
checks in the negative to be way less intuitive) and very, very
difficult to debug (hint, while developing these you can include field
references in the message (I think it's a ${} syntax but I can't
remember) to try to get some debugging info back out to yourself.)

Your other choices are...

1) Just do validation at the top of your Action's execute method.
Your Action's execute method gets called after all the interceptors have
run.  Meaning that all the form values will have been applied to the
Action's (or domain objects) properties.

So the first thing you do at the top of your execute method is a bunch
of validity checks on those properties.  If those checks fail, you just
some sort of message to the Action's FieldErrors/ActionErrors
(addActionError, addFieldError, etc.) lists and return a
failure-oriented result code (commonly INPUT).  Same end-result as if
you'd used the xml framework.

(Note: I think actions get their fieldError and ActionError lists by
sublclassing ActionSupport which implements validationAware.  So if your
actions are not doing either you'll want to check that out.)

2) Implement the "validatable" interface
Not a whole lot different than the above but in this case you put your
validation logic in a "validate" method in your action.

---

If you want to do the same validation for a domain object in several
different actions, Webwork (and presumably s2) has the option to shift
XML-based validation to be domain-object-based (instead of being
action-based).  Basically you write a -validation.xml file for the
*domain* object not each of the actions and then you tell the action to
delegate the validation back to the domain object.  I've completely
forgotten how to do this but if that interests you you should be able to
find it in the docs easily enough.

In general, what you want to do (multi-field validation) is common,
directly supported, and fairly easy.  You just need to pick which route
you want to go.  I'd probably recommend doing this in the "validate"
method.  You get to use java, and it's absolutely clear that this is
"validation" logic (because it's in a dedicated validate method).

That is, I'd recommend it if you're OK tying your validation logic to
the actions.  If you want validation logic you can share across more
tiers of your application that's an entirely separate discussion.

WARNING: the above was true for WebWork (2.x).  Or mostly.  It's been
awhile since I've looked at any of this in depth and I'm going from
memory here.  (and right-before-bed memory at that).  But the above
should be correct enough to get you headed in the right direction.

Good luck!

- Gary




:-)


-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ------
To unsubscribe, e-mail: user-unsubscribe@(protected)
For additional commands, e-mail: user-help@(protected)


©2008 junlu.com - Jax Systems, LLC, U.S.A.