Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

validation - 如何在自定义验证注释上使用椭圆形(how to use oval on custom validation Annotation)

Here is my code for creating a custom Annotation for validating Name

(这是我的代码,用于创建用于验证名称的自定义注释)
ValidName.java

(ValidName.java)

package custom.Annotation;
import java.lang.annotation.*;
import net.sf.oval.configuration.annotation.Constraint;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.LOCAL_VARIABLE)
@Constraint(checkWith=NameValidator.class)
public @interface ValidName {
String  message() default NameValidator.message;
}

here is my code for Constraint Class

(这是我约束类的代码)

package custom.Annotation;
import net.sf.oval.Validator;
import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
import net.sf.oval.context.OValContext;
import net.sf.oval.exception.OValException;
import play.Logger;
import java.util.regex.Pattern;
public class NameValidator extends AbstractAnnotationCheck<ValidName>
{    
public final static String message="custom.message";
private static final String letter = "[a-zA-Z]";
public static final Pattern VALID_PATTERN = Pattern.compile(letter);

public static boolean isValidText(String userName) {
    return VALID_PATTERN.matcher(userName).matches();
}


@Override
public void configure(ValidName annotation) {
    setMessage(annotation.message());
}
@Override
public boolean isSatisfied(Object validatedObject, Object valueToValidate, OValContext context,
                           Validator validator) throws OValException {
    try
    {
        if (valueToValidate == null) {
            return false;
        }
    }catch (Exception e){
        e.getMessage();
    }
           return` isValidText(valueToValidate.toString()`); 
}
}

When I applied @ValidName to any local variable nothing happened and I also am unable to debug the program.

(当我将@ValidName应用于任何局部变量时,没有任何反应,并且我也无法调试该程序。)

Any suggestions?

(有什么建议么?)

  ask by adg translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to use the oval validation by calling validate method of oval validation library.

(您需要通过调用oval validation库的validate方法来使用oval validation 。)

@Autowired
@Qualifier("ovalValidator")
private Validator ovalValidator;            

List<ConstraintViolation> violations = null;
violations = ovalValidator.validate(objectToValidate);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...