开发者

Approach for validating 1 of n fields must be valid?

开发者 https://www.devze.com 2023-01-15 13:26 出处:网络
What\'s the preferred approach for validating that at least one of fieldA, fieldB, or fieldC is valid using a Spring 3 validator? In othe开发者_StackOverflowr words, any 2 of the fields can be invalid

What's the preferred approach for validating that at least one of fieldA, fieldB, or fieldC is valid using a Spring 3 validator? In othe开发者_StackOverflowr words, any 2 of the fields can be invalid, as long as the third is not.


This thread poses almost the same question. The solution there is to add the following to validation-rules.xml:

<validator name="requiredif" 
    classname="org.springmodules.validation.commons.Fi eldChecks"
    method="validateRequiredIf" 
    methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.springframework.validation.Errors, org.apache.commons.validator.Validator"
    msg="errors.required">
</validator> 


I use JSR-303 Bean Validation (see example here). Simply annotate the fields that you need to validate.

0

精彩评论

暂无评论...
验证码 换一张
取 消