【问题标题】:Sprint Boot, Thymeleaf and Form Input Validation - Changing the color of the error messageSpring Boot、Thymeleaf 和表单输入验证 - 更改错误消息的颜色
【发布时间】:2016-02-20 15:14:20
【问题描述】:

我的环境是使用 Thymeleaf 的 Spring Boot 1.2.7.RELEASE。

我验证了可以正常工作的字段,但我想对错误消息进行一些格式化。我似乎在服务器端的消息中输入的任何内容都会转义传递到我的页面,所以我想我必须在我的 Thymeleaf 模板中使用 css 执行此操作。

但是怎么做呢?我已经尝试了很多东西,比如:

<label for="password" title="Enter your password here">Password</label> 
<input type="password" name="password" id="password" th:field="*{password}" cssErrorClass="form-field-error" size="15"/> 
<p class="form-error-text" th:if="${#fields.hasErrors('password')}" th:errors="*{password}">There was a problem with your password</p>

这会导致根本不呈现错误文本。如果我删除 class="form-field-text" 我会收到错误消息以呈现 int eh 正确的位置。

所以我想底线是,如何将 css 类应用于验证错误文本?

【问题讨论】:

    标签: css spring-boot thymeleaf


    【解决方案1】:

    使用th:errorclass="form-field-error" 而不是cssErrorClass="form-field-error"

    http://www.thymeleaf.org/whatsnew21.html#errcl

    【讨论】:

    • 谢谢,密码字段的轮廓变红了。我可以将显示为红色的错误消息的文本也设为红色吗?
    • 你需要编辑css为p.form-error-text { color: red; }
    • 所以,这行得通:p.form-error-text{color:red;font-size:13px;} 但这不行:p.form-error-text{color:#b24747; font-size:13px;} 知道为什么吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 2020-06-04
    • 1970-01-01
    相关资源
    最近更新 更多