struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dirk Forchel <dirk.forc...@exedio.com>
Subject Re: Conditional Annotation based Validation
Date Fri, 09 Jan 2009 06:15:36 GMT

Thank you for your posting. I already know how to use the
@FieldExpressionValidator and annotations at property level (on setter
methods) or how to use the validators with annotations at method level
(@Validations for an execute() method). But I did not get it work with the
@ExpressionValidator and conditional clauses. So let's say:

privatePerson is a boolean property. If privatePerson==true do some
validation and if privatePerson==false do some different validation. For
instance, if privatePerson==true I wanna use required string validators for
the field properties "name", "number1" and "number2". But if
privatePerson==false I wanna use only the field validator for "number1". But
I miss the conditional part.

@Validations(
   requiredStrings = {
     @RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName =
"name", message = "You must enter a value for name."),
     @RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName =
"number1", message = "You must enter a value for number1."),
     @RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName =
"number2", message = "You must enter a value for number2.")
   },
   expressions = {
      @ExpressionValidator(expression = "privatePerson", message="", key="")
   }
)
public String execute() throws Exception {
        return SUCCESS;
}


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

-- 
View this message in context: http://www.nabble.com/Conditional-Annotation-based-Validation-tp21328921p21366828.html
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