Spring 在验证消息中删除引号
我正在使用Spring Thymeleaf,并且在external messages.properties文件中有表单验证消息 当我这样引用消息时:Spring 在验证消息中删除引号,spring,validation,thymeleaf,properties-file,Spring,Validation,Thymeleaf,Properties File,我正在使用Spring Thymeleaf,并且在external messages.properties文件中有表单验证消息 当我这样引用消息时: th:attr="data-error=#{field.error.required.field}" 如以下输入字段声明所示: <input class="form-control input-lg" type="text" th:field="*{firstName}" th:attr="data-error=#{field
th:attr="data-error=#{field.error.required.field}"
如以下输入字段声明所示:
<input class="form-control input-lg" type="text" th:field="*{firstName}"
th:attr="data-error=#{field.error.required.field}" required="true"
data-delay="100" placeholder="First name"/>
如何显示没有引号的消息
下面是一些更详细的代码。模型对象看起来像:
public class UserSession {
@Email
@NotEmpty
@Size(min = 2, max = 255)
private String email;
...
}
<div class="form-group" th:classappend="${#fields.hasErrors('email')} ? has-error">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
<input class="form-control input-lg" type="email" id="email"
th:field="*{email}" th:attr="data-error=#{field.error.invalid.email}"
required="true" data-delay="100" placeholder="Email"/>
</div>
<div class="help-block with-errors" th:errors="*{email}"></div>
<div class="help-block with-errors"></div>
</div>
表单输入如下所示:
public class UserSession {
@Email
@NotEmpty
@Size(min = 2, max = 255)
private String email;
...
}
<div class="form-group" th:classappend="${#fields.hasErrors('email')} ? has-error">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
<input class="form-control input-lg" type="email" id="email"
th:field="*{email}" th:attr="data-error=#{field.error.invalid.email}"
required="true" data-delay="100" placeholder="Email"/>
</div>
<div class="help-block with-errors" th:errors="*{email}"></div>
<div class="help-block with-errors"></div>
</div>
当我删除数据错误
属性并将消息直接绑定到模型对象属性时,我得到的是默认错误,而不是消息
您需要将@NotBlank/@NotNull与表单类绑定
您可以直接绑定message=“error.message”//properties变量
您的HTML代码:
<form role="form" th:action="@{/signup}" method="post" th:object="${userForm}">
<div class="row">
<div class="col-lg-12">
<th:block th:if="${#fields.hasErrors('${userForm.*}')}">
<div th:utext="Common error message">Alert</div>
</th:block>
<div class="form-group input-group" th:classappend="${#fields.hasErrors('firstName')}? 'has-error'">
<input type="text" th:field="*{firstName}" class="form-control" placeholder="firstName" />
<span class="help-block" th:if="${#fields.hasErrors('firstName')}" th:errors="*{firstName}">Incorrect title</span>
</div>
// your other filed with submit button
</div>
</div>
</form>
警觉的
标题不正确
//你的另一个提交按钮
注:
- 如果表单验证失败,则在标头中附加公共消息
- 表单字段错误消息根据表单类中设置的消息追加