Krzysztof Majewski
Krzysztof Majewski

Reputation: 2534

Spring MVC form validation wrong result

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?

Upvotes: 0

Views: 431

Answers (1)

G.Spansky
G.Spansky

Reputation: 902

@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);

Upvotes: 1

Related Questions