0

I am using Spring MVC + thymeleaf and when I am submitting my form it gives me all the time validation error(Null) in the binding result. Any advise would be appreciated! Thanks!

Error message

"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;
}
}

And my 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>
3
  • are you passing any Author ID?
    – Anoop LL
    Commented Aug 30, 2016 at 7:02
  • @NotNull isn't null protection service. It's your contract which says that you will manage by yourself not to put null there.
    – xenteros
    Commented Aug 30, 2016 at 7:04
  • 1
    ${#fields.hasErrors('id')} or ${#fields.hasErrors('authorID')}
    – Aroniaina
    Commented Aug 30, 2016 at 7:08

1 Answer 1

1

As written in the comment to the question:

Annotation @NotNull won't protect you from null. It's your own contract with you and other developers working on this code that there can't be null there.

This means that you have to validate data by yourself. There are two approaches. You can create AuthorTO which will allow nulls and then create Author if and only if there are no unexpected nulls or validate on front-end side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.