MJ-79
MJ-79

Reputation: 133

@RequiredStringValidator using an expression

I'm using struts 2 & validation annotations. I need to validate a field which is required if another is true or false. I was using @RequiredStringValidator but I realized this annotation doesn't have the expression property.

    @Validations(requiredStrings =
{@RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "username", message = "El Usuario es requerido",key="security.admin.user.username.required"),
 @RequiredStringValidator( type = ValidatorType.SIMPLE, fieldName = "password", message = "El Password es requerido",key="security.admin.user.password.required")}...

The field to validate is "password" but it's only required if "autoGeneratePassword" field is false.

Thanks!

Upvotes: 0

Views: 661

Answers (1)

mprabhat
mprabhat

Reputation: 20323

For validations requiring expression evaluation don't use Simple RequiredStringValidation instead you can use FieldExpressionValidator or ExpressionValidator

One way of using it:

@FieldExpressionValidator(fieldName="password",key="security.admin.user.password.required",message="El Password es requerido",expression="checkAutoPassword()")

private boolean checkAutoPassword(){
  return false;
}

You can use OGNL expression to do the same.

Upvotes: 1

Related Questions