spring thymeleaf 删除验证消息中的引号
spring thymeleaf remove quotes in validation message
我正在使用 Spring Thymeleaf,我在外部 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.error.required.field}" required="true"
data-delay="100" placeholder="First name"/>
浏览器中出现验证信息时,带有引号,如下所示:
"This is a required field"
如何显示不带引号的消息?
这里是一些更多的代码细节。模型对象如下所示:
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>
当我删除 data-error
属性并将消息直接绑定到模型对象 属性 时,我得到的是默认错误,而不是我的消息。
You need to bind @NotBlank/@NotNull with your form class
you can bind directly message = "error.message" // your properties variable
public class UserForm implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@NotBlank(message = "error.message")
private String firstName;
@NotBlank(message = "error.message")
private String lastName;
// your other filed with Getters and Setters
...........................
}
您的 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>
注:
- 如果表单验证失败 header 中追加通用消息
- 根据您在表单 class
中设置的消息附加表单提交的错误消息
我正在使用 Spring Thymeleaf,我在外部 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.error.required.field}" required="true"
data-delay="100" placeholder="First name"/>
浏览器中出现验证信息时,带有引号,如下所示:
"This is a required field"
如何显示不带引号的消息?
这里是一些更多的代码细节。模型对象如下所示:
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>
当我删除 data-error
属性并将消息直接绑定到模型对象 属性 时,我得到的是默认错误,而不是我的消息。
You need to bind @NotBlank/@NotNull with your form class
you can bind directly message = "error.message" // your properties variable
public class UserForm implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@NotBlank(message = "error.message")
private String firstName;
@NotBlank(message = "error.message")
private String lastName;
// your other filed with Getters and Setters
...........................
}
您的 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>
注:
- 如果表单验证失败 header 中追加通用消息
- 根据您在表单 class 中设置的消息附加表单提交的错误消息