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
Servlet : Session invalidate
Oracle Connection Pooling in 3 2 2
Servlet action is currently unavailable
Tomcat/Struts Unicode Encoding/Decoding problems
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-08-13       - By j alex

 Back
I'm still wondering how to do a pure flag check alone using validators
- i.ei need to check a guard expression before firing actual
validations on the
field ; and if the guard expression fails - the validations must simply be
skipped (as if the field didn't exist) and move on to next page - without
showing any error message.

This is somewhat similar to shortcircuiting, but shortcircuit looks for the
presence of errors to stop remaining validators - and in the scenario i
described , it's not really an error, but a guard condition.

-Joseph

On 8/13/07, mraible <matt@(protected)> wrote:
>
>
> I haven't tried working with Struts 2's annotations for validation yet, so
> I'm unable to answer this question. In a week, things might be different.
> ;-)
>
> Matt
>
>
> strutstwouser wrote:
> >
> > Hi Matt,
> >
> > Can you please tell me what's needed to use this validator using
> > annotations alone? . Also, i need a simple conditional validator - ie a
> > field needs to be validated only if a prerequisite condition is
> satisfied,
> > else the validations on it must be skipped and no error must be added
> > (since this is just a condition check).
> >
> > Example : if appname is to be validated only if appid < 100, i will give
> > the inverse expression in ConditionalFieldValidator with shortCircuit
> true
> > and the subsequent validations below it ; assumption being if the first
> > condition becomes true (i.e prerequisite not satisfied), then dont
> > validate further.
> >
> > The annotation i need would look like :
> >
> > @(protected)(fieldName = "app.appname", expression =
> > "app.appid > 100", message = "", shortCircuit=true)
> > @(protected)(fieldName = "app.appname" message = "App
> Name
> > must be gt 5 chars", minLength = "5",  maxLength = "12")
> >
> > I don't know how to make Struts 2 "see" this custom validator.
> >
> > Thanks,
> > Joseph
> >
> >
> >
> > mraible wrote:
> >>
> >> I figured out how to do this - posting here so others will benefit.
> >>
> >> 1. Create a new ConditionalVisitorFieldValidator.java:
> >>
> >> public class ConditionalVisitorFieldValidator extends
> >> VisitorFieldValidator
> >> {
> >>   private String expression;
> >>
> >>   public void setExpression(String expression)
> >>   {
> >>     this.expression = expression;
> >>   }
> >>
> >>   public String getExpression()
> >>   {
> >>     return expression;
> >>   }
> >>
> >>   /**
> >>    * If expression evaluates to true, invoke visitor validation.
> >>    * @(protected) object the object being validated
> >>    * @(protected) ValidationException
> >>    */
> >>   public void validate(Object object) throws ValidationException
> >>   {
> >>     if (validateExpression(object))
> >>     {
> >>       super.validate(object);
> >>     }
> >>   }
> >>
> >>   /**
> >>    * Validate the expression contained in the "expression" paramter.
> >>    * @(protected) object the object you're validating
> >>    * @(protected) true if expression evaluates to true (implying a
> validation
> >> failure)
> >>    * @(protected) ValidationException if anything goes wrong
> >>    */
> >>   public boolean validateExpression(Object object) throws
> >> ValidationException
> >>   {
> >>     Boolean answer = Boolean.FALSE;
> >>     Object obj = null;
> >>
> >>     try
> >>     {
> >>       obj = getFieldValue(expression, object);
> >>     }
> >>     catch (ValidationException e)
> >>     {
> >>       throw e;
> >>     }
> >>     catch (Exception e)
> >>     {
> >>       // let this pass, but it will be logged right below
> >>     }
> >>
> >>     if ((obj != null) && (obj instanceof Boolean))
> >>     {
> >>       answer = (Boolean) obj;
> >>     }
> >>     else
> >>     {
> >>       log.warn("Got result of " + obj + " when trying to get
> Boolean.");
> >>     }
> >>
> >>     return answer;
> >>   }
> >> }
> >>
> >> 2. Add it to your validators.xml:
> >>
> >> <validator name="conditionalvisitor"
> >> class="com...validation.ConditionalVisitorFieldValidator"/>
> >>
> >> 3. Write your validation rule:
> >>
> >>   <field name="colleaguePosition">
> >>     <field-validator type="fieldexpression" short-circuit="true">
> >>       reason == 'colleague' and colleaguePositionID == '_CHOOSE_'
> >>       <message>You must choose a position where you worked with this
> >> person, or choose "Other..."</message>
> >>     </field-validator>
> >>     <field-validator type="conditionalvisitor">
> >>       reason == 'colleague' and colleaguePositionID == 'OTHER'
> >>       <message/>
> >>     </field-validator>
> >>   </field>
> >>
> >> Hope this helps,
> >>
> >> Matt
> >>
> >>
> >> mraible wrote:
> >>>
> >>> I need to do something similar - is it possible to have conditional
> >>> visitor validation in Struts 2? AFAICT, it isn't.
> >>>
> >>> Basically, I'd like to have a couple of validation rules for a
> >>> drop-down. One rule is that the user must select at least one choice
> >>> when the drop-down has its radio button selected:
> >>>
> >>>     <field-validator type="fieldexpression" short-circuit="true">
> >>>         reason != 'partner' or (reason == 'partner' and
> >>> partnerPositionID != '_CHOOSE_')
> >>>         <message>You must choose a position where you worked with this
> >>> person, or choose "Other..."</message>
> >>>     </field-validator>
> >>>
> >>> This works. Now I want to require a number of fields if the person
> >>> selects the "Other..." option. The validation syntax starts to get
> >>> complicated at this point. I'd like to do something like:
> >>>
> >>> reason != 'partner' or (reason == 'partner' and partnerPositionID !=
> >>> 'OTHER')  -> kick in visitor validation.
> >>>
> >>> I still think the above syntax is confusing (ref
> >>> http://tinyurl.com/2htw2k), I'd much rather write something like:
> >>>
> >>> reason == 'partner' and partnerPositionID == 'OTHER' -> show message
> >>>
> >>> I'm guessing it's possible to write my own FieldExpressionValidator
> that
> >>> inverses the true/false outcome?
> >>>
> >>> Why do I need conditional visitor validation?
> >>>
> >>> I'm trying to create a "component" that can be re-used in the backend
> >>> (model object w/ its own validation rules) and on the front-end (using
> >>> JSP tag files or the s:component tag).
> >>>
> >>> If there's an easier way to do this, please let me know. Of course, I
> >>> could use JSF/Wicket/Tapestry - and that might be the outcome if this
> is
> >>> not possible.
> >>>
> >>> Thanks,
> >>>
> >>> Matt
> >>>
> >>>
> >>> Sparecreative wrote:
> >>>>
> >>>> Is there a way to setup conditional validation through the
> >>>> validation.xml
> >>>> file?
> >>>>
> >>>> I'm currently user the visitor validator method where my core user
> >>>> validation properties are in a User-validation.xml file. I want to be
> >>>> able
> >>>> to use this same file for all my user actions (register, add, update)
> >>>> and
> >>>> just have conditional code which looks a user field to determine
> >>>> validation.
> >>>>
> >>>> At the moment I'm using a combination of the valididation.xml file
> and
> >>>> the
> >>>> validate method in the action.
> >>>>
> >>>> I know the following doesn't work, but can I have something like
> this:
> >>>>
> >>>> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator
> >>>> 1.0.2//EN"
> >>>> "http://www.opensymphony.com/xwork/xwork-validator-1 (See http://tor-1.ora-code.com).0.2.dtd">
> >>>> <validators>
> >>>>
> >>>>  <validator type="expression">
> >>>>
> >>>>       user.action.equals('insert') || user.action.equals('register')
> >>>>  <!-- only validate these fileds when inserting and registering a
> user
> >>>> -->
> >>>>  <field name="user.password">
> >>>>     <field-validator type="requiredstring">
> >>>>       <message key="user.password.empty"/>
> >>>>     </field-validator>
> >>>>   </field>
> >>>>   <field name="user.confirmPassword">
> >>>>     <field-validator type="requiredstring">
> >>>>       <message key="user.confirmPassword.empty"/>
> >>>>     </field-validator>
> >>>>   </field>
> >>>>  </validator>
> >>>> <!-- core validated fields -->
> >>>>     <field name="user.name">
> >>>>         <field-validator type="requiredstring">
> >>>>         <message key="user.name.empty">
> >>>>             resource not found</message>
> >>>>         </field-validator>
> >>>>     </field>
> >>>>     <field name="user.email">
> >>>>         <field-validator type="requiredstring"
> >>>>          short-circuit="true">
> >>>>             <message key="user.email.empty"/>
> >>>>         </field-validator>
> >>>>         <field-validator type="email">
> >>>>             <message key="user.email.invalid"/>
> >>>>         </field-validator>
> >>>>     </field>
> >>>>     <field name="user.phone">
> >>>>         <field-validator type="stringlength">
> >>>>         10
> >>>>         <message key="user.phone.length"/>
> >>>>         </field-validator>
> >>>>     </field>
> >>>>     <field name="user.city">
> >>>>         <field-validator type="requiredstring">
> >>>>             <message key="user.city.empty"/>
> >>>>         </field-validator>
> >>>>     </field>
> >>>>
> >>>>
> >>>>
> >>>> Z.
> >>>>
> >>>>
> >>>>
> >>>> -- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ------
> >>>> To unsubscribe, e-mail: user-unsubscribe@(protected)
> >>>> For additional commands, e-mail: user-help@(protected)
> >>>>
> >>>>
> >>>>
> >>>
> >>>
> >>
> >>
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/Conditional-validation-tf3678771.html#a12131596
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> -- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ------
> To unsubscribe, e-mail: user-unsubscribe@(protected)
> For additional commands, e-mail: user-help@(protected)
>
>

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