【发布时间】:2019-01-15 02:17:10
【问题描述】:
我是 Java 新手。使用 Thymeleaf 和 Spring-Boot。尝试在错误输入时显示验证消息。 “电话”属性必须介于 7 到 11 个字符 int 之间。 如果不遵守规则,将显示验证消息。她是
模型
@Entity
@Table(name="users")
public class User {
@Column(name="phone")
@Size(min=7 , max=11 , message="Invalid Number")
private int phone;
}
我的控制器
@PostMapping( "/signup" )
public String signupPost(@Valid @ModelAttribute("user") User user ,
Model model , BindingResult thebindResult ) {
if(thebindResult.hasErrors()) {
model.addAttribute("invalidPhone" , true);
return "signup";
}
我的看法
<div class="form-group">
<label for="phone" class="cols-sm-2 control-
label">Phone</label><span class="bg-danger pull-right"
th:if="${invalidPhone}" th:errors="{*'phone'}" >phone
Error</span>
<div class="cols-sm-10">
<div class="input-group" >
<span class="input-group-addon" ><i class="fa
fa-phone fa" aria-hidden="false" ></i> </span>
<input type="text" class="form-control"
th:value="${user.phone}" id="phone" name="phone"
roleId="phone"
th:field="*{phone}" placeholder="xxx-xxx-xxxx"
required="required"/>
</div>
</div>
</div
错误
Neither BindingResult nor plain target object for bean name 'phone'
available as request attribute
【问题讨论】:
-
您可以在您的 HTML 和您的
@GetMapping方法上发布您的form标记吗? -
-
错字:
{*'phone'}应该是*{phone}。 -
它不工作仍然有同样的错误?
-
Whitelabel 错误页面 此应用程序没有明确的 /error 映射,因此您将其视为后备。 Wed Aug 08 14:51:56 EET 2018 出现意外错误(类型=内部服务器错误,状态=500)。 HV000030:找不到约束“javax.validation.constraints.Size”验证类型“java.lang.Integer”的验证器。检查“电话”的配置
标签: hibernate validation spring-boot thymeleaf