2017-06-26 7 views
1

@Validアノテーションは、ポストリクエストがコントローラメソッドに到着したときに検証を開始しません。私は何をすべきか ?Thymeleaf Spring Bootで@Validフォームの検証が機能しない

親コントローラー:

public class ApplicationController { 

    @Autowired 
    private I18NService i18NService; 

    public I18NService getI18NService() { 
     return i18NService; 
    } 

    public void setI18NService(I18NService i18NService) { 
     this.i18NService = i18NService; 
    } 
} 

レイアウトコントローラ:

@Controller 
public class ClienteController extends ClientLayoutController { 

    @GetMapping("/client/profile") 
    public ModelAndView clientProfile() { 
     ModelAndView mav = this.getModelAndView("profile","client.profile","store/account-profile"); 
     return mav; 
    } 

    @PostMapping("/client/profile") 
    public ModelAndView clientProfileUpdate(@Valid Cliente cliente,BindingResult bindingResult,Model model) { 
     System.out.println("sdfsdf "+cliente.getNome()); 
     System.out.println(bindingResult.getErrorCount()); 

     if(bindingResult.hasErrors()){ 
      ModelAndView mav = this.getModelAndView("profile","client.profile","store/account-profile"); 
      mav.addObject("cliente",cliente); 
      return mav; 
     } 

     return this.getModelAndView("profile","client.profile","store/account-profile"); 
    } 

} 

Thymeleafフォーム::

public class ClientLayoutController extends ApplicationController{ 

    @Autowired 
    private UserService userService; 

    @Autowired 
    private ClienteService clienteService; 

    protected ModelAndView getModelAndView(String aActiveMenu, String aI18n, String aViewName){ 
     ModelAndView mav = new ModelAndView(); 
     User user = userService.getAuthenticatedUser(); 
     Cliente cliente = clienteService.getAuthenticatedCliente(); 

     mav.addObject("active_menu",aActiveMenu); 
     mav.addObject("titulo",this.getI18NService().getMessage(aI18n)); 
     mav.addObject("user",user); 
     mav.addObject("cliente",cliente); 
     mav.setViewName(aViewName); 
     return mav; 
    } 
// Getters ans Setters... 
} 

要求が入ってきコントローラー

<form th:method="post" th:action="@{'/client/profile'}" th:object="${cliente}"> 
    <div class="form-group"> 
     <label for="nomecli" th:text="#{client.profile.name}">First Name</label> 
     <input th:field="*{nome}" type="text" id="nomecli" class="form-control" th:placeholder="#{client.profile.name}"/> 
     <span th:if="${#fields.hasErrors('nome')}" th:errors="*{nome}" class="text-danger"></span> 
    </div>    
    <button type="submit" class="btn btn-default btn-theme" th:inline="text"><i class="fa fa-check"></i> [[#{crud.save}]]</button> 
</form> 

エンティティ:

@Entity(name = "cliente") 
public class Cliente { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id; 

    @Column(name = "nome") 
    @NotEmpty(message = "Informe o nome") 
    private String nome; 

// Other fields... 
} 

bindingResult.getErrorCountは()私は空白のフォームを投稿するにもかかわらず、常に0です。私は@ NotNullとGoogleで他の多くのものを追加しようとしましたが、成功はありません。次のように

答えて

1

は、コントローラの署名に@ModelAttribute("cliente")を追加します。一方

public ModelAndView clientProfileUpdate(@Valid @ModelAttribute("cliente") Cliente cliente, BindingResult bindingResult,Model model) {...} 

を、あなたは、データベースのエントリを表すために使用されるビューにEntityを渡しています。単純なPOJOクラスのデータ転送オブジェクトを使用し、@NotNullまたは@NotEmpty注釈をフィールドに追加します。

+0

それは私のために動作しますが、あなたの助けに感謝しませんでした!それはcontrolelrの継承とは関係ありませんか?オンラインで見てきた唯一の事は、これだけです。私はもう少し時間があり、あなたに知らせるときに、後で試してみます。 – viniciusalvess

+0

私はそれを、@ balag3から取り出しました。後でそれをチェックしてください。 – viniciusalvess

2

私がアプリケーション上に持っていたすべてのカスタムバリデータを削除し、自分の他のコントローラ上にあるすべての@InitBindersを削除してカスタム検証を行いました。 @Validは期待された動作をします。 私はそれを自分で見つけた後、以下の投稿がより良く説明されています。

Spring - mixing annotation and validator-based validations

関連する問題