yangys
2024-09-04 04c57331cf84c8f606c2838dcb6fe5463fb9b68c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.qianwen.smartman.common.validator.phone;
 
import cn.hutool.core.util.StrUtil;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
 
public class PhoneValueValidator implements ConstraintValidator<PhoneValue, String> {
    private Boolean required;
 
    public void initialize(PhoneValue constraintAnnotation) {
        this.required = Boolean.valueOf(constraintAnnotation.required());
    }
 
    public boolean isValid(String phoneValue, ConstraintValidatorContext context) {
        if (!StrUtil.isEmpty(phoneValue)) {
            return phoneValue.length() == 11;
        } else if (this.required.booleanValue()) {
            return false;
        } else {
            return true;
        }
    }
}