2016-08-30 6 views
0

私はSpring MVC + thymeleafを使用しています。フォームを送信しているときに、バインディング結果にすべての検証エラー(Null)が表示されます。どんなアドバイスもいただければ幸いです!ありがとう!フォームはサブミット時にヌルを返します。

エラーメッセージ

"Field error in object 'createForm' on field 'authorId': rejected value [null]; codes [NotNull.createForm.authorId,NotNull.authorId,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [createForm.authorId,authorId]; arguments []; default message [authorId]]; default message [may not be null]" 
@NotNull 
@Size(min= 2, max = 100, message = "your title should be between 2 and 100 symbols") 
private String title; 

@NotNull 
@Size(min = 2, message = "Please fill your message") 
private String content; 

@DateTimeFormat(pattern = "yyyy-mm-dd") 
private Date date; 

@NotNull 
private String authorId; 



public String getTitle() { 
    return title; 
} 

public void setTitle(String title) { 
    this.title = title; 
} 

public String getContent() { 
    return content; 
} 

public void setContent(String content) { 
    this.content = content; 
} 

public Date getDate() { 
    return date; 
} 

public void setDate(Date date) { 
    this.date = date; 
} 

public String getId() { 
    return authorId; 
} 

public void setId(String authorId) { 
    this.authorId = authorId; 
} 
} 

そして、私のHTML

<form id="create-form" method="post" th:object="${createForm}"> 
    <div><label for="title">Title:</label></div> 
    <input id="title" type="text" name="title" th:value="*{title}"/> 
    <span class="formError" th:if="${#fields.hasErrors('title')}" th:errors="*{title}">Invalid title</span> 
    <div><label for="content">Content:</label></div> 
    <textarea name="content" rows="30" cols="100" id="content" th:value="*{content}"></textarea> 
    <span class="formError" th:if="${#fields.hasErrors('content')}" th:errors="*{content}">Invalid content</span> 
    <div><label for="date">Date:</label></div> 
    <input id="date" type="date" name="date" th:value="*{date}"/> 
    <span class="formError" th:if="${#fields.hasErrors('date')}" th:errors="*{date}">Invalid date</span> 
    <div><label for="authorId">Author ID:</label></div> 
    <input id="authorId" type="text" name="authorId" th:value="*{authorId}"/> 
    <span class="formError" th:if="${#fields.hasErrors('id')}" th:errors="*{authorId}">Invalid id</span> 

    <br/> 
    <br/> 
    <div><input type="submit" value="Create"/></div> 
</form> 
+0

あなたは著者IDを渡していますか? –

+0

'@ NotNull'は'ヌルプロテクションサービス 'ではありません。それはあなたが自分でそれで 'ヌル 'を置かないように管理すると言うあなたの契約です。 – xenteros

+1

$ {#fields.hasErrors( 'id')}または$ {#fields.hasErrors( 'authorID')} – Aroniaina

答えて

1

質問へのコメントに書かれたよう:

注釈@NotNullnullからあなたを保護しません。あなたとこのコードで作業している他の開発者とのあなた自身の契約です。そこにはnullはありません。

これは、自分でデータを検証する必要があることを意味します。 2つのアプローチがあります。 nullを許可するAuthorTOを作成し、予期しないものがあった場合にのみAuthorを作成してください。nullまたはフロントエンドで検証してください。

関連する問題