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 Fri, 03 Aug 2007 21:14:08 GMT

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#a11990883
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