0

I've made a simple form and I am trying to validate it but I have some trouble. This is my controller:

import ninja.majewski.store.forms.ContactDTO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;

@Controller
public class MainController {

    @RequestMapping(value = "/contact", method = RequestMethod.GET)
    public String contact(Model model) {
        addBasicInfo(model);

        model.addAttribute("form", new ContactDTO());

        return "contact";
    }

    @RequestMapping(value = "/contact", method = RequestMethod.POST)
    public String contact(Model model, @ModelAttribute("form") @Valid ContactDTO form, BindingResult result) {
        addBasicInfo(model);

        model.addAttribute("form", new ContactDTO());

        // return false
        System.out.println(result.hasErrors());

        if (result.hasErrors()) {
            return "contact";
        } else {
            return "redirect:/home";
        }
    }

}

This is my DTO class:

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;

import javax.validation.constraints.Size;

public class ContactDTO {

    @NotEmpty
    @Size(min = 3)
    private String name;

    @NotEmpty
    @Email
    private String email;


    private String message;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

And my form HTML:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

</body>
</html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>I AM A HORSE</title>

    <spring:url value="/resources/css/bootstrap.css" var="bootstrapCss"/>
    <link href="${bootstrapCss}" rel="stylesheet" type="text/css"/>
</head>

<body>

<jsp:include page="parts/header.jsp"/>
<jsp:include page="parts/leftMenu.jsp"/>

<table>
    <td>
        <form:form action="/contact" modelAttribute="form" method="post">

            Name:
            <form:input path="name" id="name"/>
            <form:errors path="name" cssclass="error"/>
            <br/>

            Email:
            <form:input path="email" id="email"/>
            <form:errors path="email" cssclass="error"/>
            <br/>

            Message:
            <form:input path="message" id="message"/>
            <form:errors path="message" cssclass="error"/>
            <br/>

            <input type="submit" value="Send message"/>
        </form:form>
    </td>

</table>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="/resources/js/bootstrap.min.js"></script>

</body>
</html>

And my app configuration:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <mvc:annotation-driven/>
    <context:component-scan base-package="ninja.majewski"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

The problem is that even when I submit empty form BindingResult.hasErrors() gives false. Where have I made a mistake?

4
  • remove @ModelAttribute("form") . You don't need it in this case Commented May 11, 2016 at 19:08
  • Nothing changed after removing. Still doesn't work fine :/ Commented May 11, 2016 at 19:12
  • Show your configuration. Do you have <mvc:annotation-driven />? Commented May 11, 2016 at 19:22
  • I've uploaded my config. I have <mvc:annotation-driven /> Commented May 11, 2016 at 19:24

1 Answer 1

1

@ModelAttribute should be first parameter of the method. Try to replace it with Model, and declare @Valid annotation before @ModelAttribute.

 @RequestMapping(value = "/contact", method = RequestMethod.POST)
public String contact(@Valid @ModelAttribute("form") ContactDTO form, BindingResult result, Model model) {
    addBasicInfo(model);
Sign up to request clarification or add additional context in comments.

2 Comments

No it hasn't. The only requirement is that the BindingResult directly follows the @ModelAttribute annotated argument. You can even have multiple.
Yes but result.hasErrors() should give true if I submit empty form but it gives false ...

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.