1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

spring error メモ - バリデーションエラーの際フォームクラスのインスタンスがNULLになる

Posted at

error log

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

html

<form id="createForm" th:attr="action=@{/domainReport/create}" th:object="${domainReportCreateForm}" method="post" enctype="multipart/form-data">

controller

	public String create(@Validated @ModelAttribute DomainReportForm domainReportCreateForm, BindingResult result,
			Model model) {

解決

	public String create(@Validated @ModelAttribute("domainReportCreateForm") DomainReportForm domainReportCreateForm, BindingResult result,
			Model model) {
  • クラス名とインスタンス名が違うときは明示的にしなければいけない
  • パラメータ順序も大切

参考

I think the trouble because you placed BindingResult incorrect. Replace it with MultipartHttpServletRequest, it must be after the validating parameter:

@RequestMapping(method= RequestMethod.POST, value = "/create")
public String createTest(@Valid @ModelAttribute("testForm") TestForm testForm,  BindingResult bindingResult, MultipartHttpServletRequest request) throws IOException {
if (bindingResult.hasErrors()) 
    return "createtestform";
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?