views:

132

answers:

2

Hi @all! My first question as a spring newbie:

How do I show validation errors NEXT to each input/component?

Validator:

@Override
public void validate( final Object obj, final Errors e )
{
   ValidationUtils.rejectIfEmpty( e, "firstname", "error.firstname.empty" );
}

JSP:

<form:label  path="firstname">
   <spring:message code="label.firstname" />
</form:label>
<form:input  path="firstname" />
<form:errors path="firstname" /> <!-- THIS DOES NOT WORK! -->

I can show all errors by using the following view code:

<spring:hasBindErrors name="contact">
   <ul>
      <c:forEach var="error" items="${errors.allErrors}">
         <spring:message code="${error.code}"></spring:message>
      </c:forEach>
   </ul>
</spring:hasBindErrors>

Any ideas?

A: 

What you have with <form:errors path="firstname" /> works for me when I do it.

Make sure in your JSP you have the <form:form/> tag setup properly:

<form:form name="someName" commandName="commandClassName">

Also make sure that the <form:errors> field is within a <form:form> tag and that your instance variables for your command class have the correct capitalization in the JSP and getters/setters.

tkeE2036
A: 

Sorry. Still does not work for me:

JSP:

<body>
   <form:form name="myForm" commandName="contactCommand" method="post" action="addContact.html">
      <form:label  path="firstname">
         <spring:message code="label.firstname" />
      </form:label>
      <form:input  path="firstname" />
      <form:errors path="firstname" />
   </form:form>
</body>

Controller:

package bka.controller;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import bka.form.Contact;
import bka.validator.ContactValidator;

@Controller
@SessionAttributes
public class ContactController
{
   @RequestMapping( value = "/addContact", method = RequestMethod.POST )
   public String addContact( @Valid @ModelAttribute( "contact" ) final Contact contact,
                             final BindingResult result )
   {
      if( result.hasErrors() )
      {
         return "forward:contacts.html";
      }   

      System.out.println( "First Name: " + contact.getFirstname() + "Last Name:  " + contact.getLastname()
               + "Years:      " + contact.getYears() );

      return "redirect:contacts.html";
   }

   @InitBinder
   protected void initBinder( final WebDataBinder binder )
   {
      binder.setValidator( new ContactValidator() );
   }

   @RequestMapping( "/contacts" )
   public ModelAndView showContacts()
   {
      return new ModelAndView( "contact", "contactCommand", new Contact() );
   }
}
eventhorizon
In the form:form tag in the JSP use modelAttribute attribute instead of commandName setting the name of your modelAttribute like this: modelAttribute="contact". In the controller add Model model after Binding result (I don't know if it's compulsory, I always add it, but it should go after binding result in case you add it). Try to remove forward prefix in case it still doesn't work.
Javi