2011-09-26 17 views
7

实施例,在Spring MVC验证中,是否有可能一次只显示一个字段的错误消息?

我有

@NotEmpty //tells you 'may not be empty' if the field is empty 
@Length(min = 2, max = 35) //tells you 'length must be between 2 and 35' if the field is less than 2 or greater than 35 
private String firstName; 

然后我输入一个空值。

它说,“不能为空 长度必须在2到35之间”

是否可以告诉春天来验证一个每场时间?

+2

为什么?用户应该知道所有的要求。 – Bozho

+0

邑,但我只想知道它是否可能:) – Kevin

回答

8

是的,这是可能的。只要创建自己的注释是这样的:

@Documented 
@Constraint(validatedBy = {}) 
@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE }) 
@Retention(RetentionPolicy.RUNTIME) 
@ReportAsSingleViolation 
@NotEmpty 
@Length(min = 2, max = 35) 
public @interface MyAnnotation { 

    public abstract String message() default "{mypropertykey}"; 

    public abstract Class<?>[] groups() default {}; 

    public abstract Class<?>[] payload() default {}; 
} 

重要的部分是@ReportAsSingleViolation注释

0

更好的解决方案是在错误信息中加入一些标记,以便格式良好,如<br />,并使用CSS类来格式化整个消息。正如Bozho的评论所指出的,用户应该意识到所有不正确的事情。

+0

这是否意味着它不可能? – Kevin

+2

我知道这是一个旧帖子,但只是一个fyi。出于安全原因,您不希望用户在登录过程中知道用户标识/电子邮件是错误还是密码。否则,知道该ID是正确的攻击者可以只关注密码。 – Susie

4

为您现场使用自定义的约束。例如,将使用注释@StringField

@Target(ElementType.FIELD) 
@Constraint(validatedBy = StringFieldValidator.class) 
@Retention(RetentionPolicy.RUNTIME) 

public @interface StringField { 

    String message() default "Wrong data of string field"; 

    String messageNotEmpty() default "Field can't be empty"; 

    String messageLength() default "Wrong length of field"; 

    boolean notEmpty() default false; 

    int min() default 0; 

    int max() default Integer.MAX_VALUE; 

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

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

然后在StringFieldValidator类中做一些逻辑。这个类通过接口ConstraintValidator <A extends Annotation, T>实现。

public class StringFieldValidator implements ConstraintValidator<StringField, String> { 

    private Boolean notEmpty; 
    private Integer min; 
    private Integer max; 
    private String messageNotEmpty; 
    private String messageLength; 

    @Override 
    public void initialize(StringField field) { 
     notEmpty = field.notEmpty(); 
     min = field.min(); 
     max = field.max(); 
     messageNotBlank = field.messageNotEmpty(); 
     messageLength = field.messageLength(); 
    } 

    @Override 
    public boolean isValid(String value, ConstraintValidatorContext context) { 
     context.disableDefaultConstraintViolation(); 
     if (notEmpty && value.isEmpty()) { 
      context.buildConstraintViolationWithTemplate(messageNotEmpty).addConstraintViolation(); 
      return false; 
     } 
     if ((min > 0 || max < Integer.MAX_VALUE) && (value.length() < min || value.length() > max)) { 
      context.buildConstraintViolationWithTemplate(messageLength).addConstraintViolation(); 
      return false; 
     } 
     return true; 
    } 
} 

然后你可以使用注解,如:

@StringField(notEmpty = true, min = 6, max = 64, 
      messageNotEmpty = "Field can't be empty", 
      messageLength = "Field should be 6 to 64 characters size") 

而且毕竟,你必须在正确的顺序中仅显示一个错误消息。

相关问题