goupan
2024-04-03 5506e9a45e717ffcb67ec313b5a4e8206d9b3a39
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package cn.stylefeng.roses.kernel.validator.api.validators.phone;
 
import cn.hutool.core.util.StrUtil;
 
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
 
/**
 * 校验手机号是否为11位
 *
 * @author fengshuonan
 * @date 2020/10/31 14:53
 */
public class PhoneValueValidator implements ConstraintValidator<PhoneValue, String> {
 
    private Boolean required;
 
    @Override
    public void initialize(PhoneValue constraintAnnotation) {
        this.required = constraintAnnotation.required();
    }
 
    @Override
    public boolean isValid(String phoneValue, ConstraintValidatorContext context) {
 
        if (StrUtil.isEmpty(phoneValue)) {
            if (required) {
                return false;
            } else {
                return true;
            }
        } else {
            return phoneValue.length() == 11;
        }
    }
}