0

There is a form for which the phone number is sometimes required and sometimes it is not and the form is validated by validator class that spring calls. I would like that validator class to know if when the field is required and when it is ok if it's empty, but I am not sure about the correct approach to implement this. The controller and validation process looks like this:

@RequestMapping(value = "my/url", method = RequestMethod.POST)
public ModelAndView someMethod(@Valid T myForm, BindingResult bindingResult) {
    //Calling the validation...
    if (bindingResult.hasErrors()) {
        // didn't pass validation 
    } else {
        // is ok!
    }
}

The form looks like this. The idea isI could add another field that would determine if phone number is required or not, if I would find a way to use it later when validating phone field. It has the @Phone annotation:

public class MyForm implements Serializable {

    private static final long serialVersionUID = 1L;

    @Phone
    private String phone;

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

}

The @Phone annotation specifies a class that will validate the data

 @Target({CONSTRUCTOR, FIELD, METHOD, PARAMETER})
 @Retention(RUNTIME)
 @Documented
 @Constraint(validatedBy = ValidateField.class)
 public @interface Phone {

     String message() default "{default.phone.errormsg}";
     int maxLength() default 64;

     Class<?>[] groups() default {};
     Class<? extends Payload>[] payload() default {};
 }

Here is where the problem starts. The class that validates data looks like this and the thing is that I would like to be able to check if the phone is required this time or not in this form, but I don't see any way to pass other form fields in this class other than the phone number value and I can't access form object here anyway:

public class PhoneValidator implements ConstraintValidator<Phone, String> {

    //...

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {

        // I want to check if the "required" field was passed from form here
        // to know if it's ok for the field to be empty
        if (StringUtils.isEmpty(value) /* && isNotRequired */) {
            return true;
        }

        return notBlank(value, context)
                && maxLength(value, context, maxLength) /* and other constraints... */;
    }

   //...
}

I am new Spring validation stuff and I am not even sure if I am going in the right direction, I would be thank full if someone could guide me to a proper way of solving this issue.

tereško
  • 58,060
  • 25
  • 98
  • 150
Javatar
  • 665
  • 2
  • 10
  • 17
  • 1
    possible duplicate of [JSR 303 Validation, If one field equals "something", then these other fields should not be null](http://stackoverflow.com/questions/9284450/jsr-303-validation-if-one-field-equals-something-then-these-other-fields-sho) – Pavel Horal May 19 '14 at 15:05
  • Actually this is not about Spring, but rather JSR-303, aka. Bean Validation. It is not possible to access other fields of the parent bean in constraint validators (as far as I know). Instead you need to define bean level constraint, which contains the desirect logic. – Pavel Horal May 19 '14 at 15:05

0 Answers0