我的验证消息在 Thymeleaf 中不起作用 [英] My Validation message not working in Thymeleaf

查看:85
本文介绍了我的验证消息在 Thymeleaf 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Java 新手.使用 Thymeleaf 和 Spring-Boot.试图在错误输入上显示验证消息.电话"属性必须介于 7 到 11 个字符之间.如果不遵守规则,将显示验证消息.她是

模型

@Entity@Table(名称=用户")公共类用户{@Column(名称=电话")@Size(min=7 , max=11 , message="无效数字")私人电话;

}

我的控制器

 @PostMapping( "/signup" )public String signupPost(@Valid @ModelAttribute("user") User user ,模型模型, BindingResult thebindResult ) {if(thebindResult.hasErrors()) {model.addAttribute("invalidPhone" , true);返回注册";}

我的观点

 

<label for="phone" class="cols-sm-2 control-label">Phone</label><span class="bg-danger pull-right"th:if="${invalidPhone}" th:errors="{*'phone'}" >phone错误<div class="cols-sm-10"><div class="input-group" ><span class="input-group-addon" ><i class="fafa-phone fa" aria-hidden="false" ></i> </span><input type="text" class="form-control"th:value="${user.phone}" id="phone" name="phone"角色 ID =电话"th:field="*{phone}" placeholder="xxx-xxx-xxxx"必需=必需"/>

</div

错误

 bean name 'phone' 既不是 BindingResult 也不是普通目标对象可用作请求属性

解决方案

从控制器,编辑如下.

@PostMapping( "/signup" )public String signupPost( @ModelAttribute @Valid User user ,BindingResult thebindResult, Model model) {if(thebindResult.hasErrors()) {model.addAttribute("invalidPhone", true);返回注册";}}

来自视图编辑

<label for="电话"class="cols-sm-2 control-label">Phone</label><span class="bg-danger pull-right";th:if=${invalidPhone}">手机错误</span><div class="cols-sm-10"><div class="input-group";><span class="input-group-addon";><i class=fa fa-phone fa"咏叹调隐藏=假";></i></span><输入类型=文本"类=表单控制"th:field="*{电话}";占位符=xxx-xxx-xxxx";required=必需"/>

</div

注意确保您的@GetMapping() 控制器显示注册,您正在将用户传递给模型即.

@GetMapping 控制器........

model.addAttribute(userModel", new User());…………

通知

//注册输入...</表单>

I am new at Java. Using Thymeleaf and Spring-Boot. Trying to show validation message on wrong input. "Phone" property must be between 7 to 11 character int. Validation message will be shown if rules are not followed. her is

the model

@Entity
@Table(name="users")
public class User  {
@Column(name="phone")
@Size(min=7 , max=11 , message="Invalid Number")
private int phone;

}

my controller

  @PostMapping( "/signup" )
  public String signupPost(@Valid @ModelAttribute("user")  User user ,
          Model model , BindingResult thebindResult ) {

    if(thebindResult.hasErrors()) {
        model.addAttribute("invalidPhone" , true);
        return "signup";

    }

my view

                <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

The Error

     Neither BindingResult nor plain target object for bean name 'phone' 
     available as request attribute

解决方案

From Controller, edit as below.

@PostMapping( "/signup" )
public String signupPost( @ModelAttribute @Valid User user ,BindingResult thebindResult, Model model) {
    if(thebindResult.hasErrors()) {
        model.addAttribute("invalidPhone" , true);
        return "signup";
    }
 }

From View Edit

<div class="form-group">
  <label for="phone" class="cols-sm-2 control-label">Phone</label>
  <span class="bg-danger pull-right" th:if="${invalidPhone}">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:field="*{phone}" placeholder="xxx-xxx-xxxx" required="required"/>
    </div>
  </div>
</div

Note Make sure your @GetMapping() Controller display Signup, u are passing User to Model ie.

@GetMapping Controller ........

model.addAttribute("userModel", new User());

........

Inform

<form method="post" action="" th:object="${userModel}">
  //signup input...
</form>

这篇关于我的验证消息在 Thymeleaf 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆