You can use @FieldExpressionValidator and @ExpressionValidator annotations
which allow you to write complex conditions.

Here is an example a plucked from my code:

    @FieldExpressionValidator(expression = "!create ||
!password.trim().isEmpty()", message = "required", key =
ERROR_PASSWORD_REQUIRED)
    public void setPassword(String password)
    {
        _password = password;
    }

If the expression is false then the validation fails, so with reversed logic
this says: if doing a create then password is required.

IMHO, once you reach a certain level of complexity its much easier to write,
debug, and test validation logic in java. 


Dirk Forchel wrote:
> 
> Finally I followed your advice and use Java-based validation now. I did
> not get it work with the expression validator via annotations. If somebody
> knows how to do the following Java-based validation with annotations just
> let me know:
> 
> public void validate() 
> {     
>    if (privatePerson) 
>    {                                  
>       if (GenericValidator.isBlankOrNull(name))
>          addFieldError("name", "name required");
>       if (GenericValidator.isBlankOrNull(number1))
>          addFieldError("number1", "number1 required");
>       else if (!(GenericValidator.isInt(number1) ||
> GenericValidator.isInt(number2)))
>          addFieldError("number1", "The number can contain only digits."); 
>       else if (!(GenericValidator.minLength(number1, 6) &&
> GenericValidator.maxLength(number1, 6)))
>          addFieldError("number1", "The first part of the number must be 6
> digits long.");
>       else if (!(GenericValidator.minLength(number2, 4) &&
> GenericValidator.maxLength(number2, 4)))
>          addFieldError("number2", "The second part of the number must be 4
> digits long.");               
>    }
>    else
>    {                          
>       if (GenericValidator.isBlankOrNull(number1))                            
>                         
>          addFieldError("number1", "number required");
>       else if (!GenericValidator.isInt(number1))
>          addFieldError("number1", "The number can contain only digits.");
>       else if (!(GenericValidator.minLength(number1, 8) &&
> GenericValidator.maxLength(number1, 8)))
>          addFieldError("number1", "The number must be 8 digits long.");
>    }
> }
> 
> 
> 
> 
> newton.dave wrote:
>> 
>> AFAIK the expression validator is available via annotations. You can 
>> also use a custom validator but I find it a little clumsy with
>> annotations.
>> 
>> Personally, as soon as validation gets even remotely complicated I fall 
>> back to using Java-based validation--I just think it's easier to 
>> maintain and much more clear.
>> 
>> I'll sometimes use a combination of both Java-based and either XML or 
>> annotations if it makes sense to.
>> 
>> On a side note, IIRC the "trim" attribute/parameter only applies the 
>> trim during validation--not on the value set on the action property.
>> 
>> Dave
>> 
>> Dirk Forchel wrote:
>>> I have two radio buttons for a form property named "privatePerson" with
>>> two
>>> possible values ('true' and 'false') which toogles the view of my input
>>> form
>>> with three different input fields (number1, number2, name).
>>> If the user choose "private" the number1, number2 and name input fields
>>> are
>>> shown, if the user choose "non-private" only the number1 input field is
>>> shown.
>>> For the first choise I would use annotation based validation like
>>> 
>>> @Validations(
>>>    requiredStrings = {...@requiredstringvalidator(type =
>>> ValidatorType.SIMPLE,
>>> fieldName = "name", message = "You must enter a name.")},
>>>    stringLengthFields = {...@stringlengthfieldvalidator(type =
>>> ValidatorType.SIMPLE, trim = true, minLength="6", maxLength = "6",
>>> fieldName
>>> = "number1", message = "The first part of the number must be 6 digits
>>> long."), @StringLengthFieldValidator(type = ValidatorType.SIMPLE, trim =
>>> true, minLength="4", maxLength = "4", fieldName = "number2", message =
>>> "The
>>> second part of the number must be 4 digits long."),
>>>                     }            
>>>     )
>>> public String execute() throws Exception {
>>> ...
>>> }
>>> 
>>> If the user selects "non-private" only the following validator should be
>>> used.
>>> 
>>> @Validations(
>>>     stringLengthFields = {...@stringlengthfieldvalidator(type =
>>> ValidatorType.SIMPLE, trim = true, minLength="8", maxLength = "8",
>>> fieldName
>>> = "number1", message = "The first part of the number must be 8 digits
>>> long."),
>>> }            
>>>     )
>>> public String execute() throws Exception {
>>> ...
>>> }
>>> 
>>> 
>>> How can I accomplish this conditional annotation based validation?
>> 
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
>> For additional commands, e-mail: user-h...@struts.apache.org
>> 
>> 
>> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Conditional-Annotation-based-Validation-tp21328921p21357126.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to