struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mraible <m...@raibledesigns.com>
Subject Re: Conditional validation
Date Mon, 13 Aug 2007 18:41:26 GMT

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 : 
> 
> @ConditionalFieldValidator(fieldName = "app.appname", expression =
> "app.appid > 100", message = "", shortCircuit=true)
> @StringLengthFieldValidator(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.
>>    * @param object the object being validated
>>    * @throws ValidationException
>>    */
>>   public void validate(Object object) throws ValidationException
>>   {
>>     if (validateExpression(object))
>>     {
>>       super.validate(object);
>>     }
>>   }
>> 
>>   /**
>>    * Validate the expression contained in the "expression" paramter.
>>    * @param object the object you're validating
>>    * @return true if expression evaluates to true (implying a validation
>> failure)
>>    * @throws 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.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@struts.apache.org
>>>> For additional commands, e-mail: user-help@struts.apache.org
>>>> 
>>>> 
>>>> 
>>> 
>>> 
>> 
>> 
> 
> 

-- 
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@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message