[英]In Spring MVC validation, Is it possible to show only one error message per field at a time?
Example, 例,
I have 我有
@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;
Then I input an empty value. 然后我输入一个空值。
It says, 'may not be empty length must be between 2 and 35' 它说''可能不是空的长度必须在2到35之间'
Is it possible to tell spring to validate one at a time per field? 是否有可能告诉spring每个字段一次验证一个?
Yes it is possible. 对的,这是可能的。 Just create your own annotation like this:
只需像这样创建自己的注释:
@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 {};
}
important part is the @ReportAsSingleViolation annotation 重要的部分是@ReportAsSingleViolation注释
Use custom constraints for your field. 为您的字段使用自定义约束。 For example, will use annotate
@StringField
. 例如,将使用注释
@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 {};
}
Then make some logic in StringFieldValidator
class. 然后在
StringFieldValidator
类中创建一些逻辑。 This class implemented by an interface ConstraintValidator <A extends Annotation, T>
. 该类由接口
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;
}
}
Then you can use annotation like: 然后你可以使用注释:
@StringField(notEmpty = true, min = 6, max = 64,
messageNotEmpty = "Field can't be empty",
messageLength = "Field should be 6 to 64 characters size")
And after all, you will have only one error message shown in right order. 毕竟,您将只有一个错误消息以正确的顺序显示。
A better solution would be to have some markup in your error message so that it formats nicely, such as a <br />
, and use a CSS class to format the overall message. 更好的解决方案是在错误消息中添加一些标记,以便格式化,例如
<br />
,并使用CSS类格式化整个消息。 As noted in Bozho's comment, a user should be aware of everything that is incorrect. 正如Bozho评论中所指出的,用户应该知道一切不正确的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.