Results 1 to 2 of 2

Thread: BindingResult not populating errors at the view.

  1. #1

    Default BindingResult not populating errors at the view.

    Hello everyone.

    By some reason I cannot get to work the BindingResult on the POST of my form. I can submit the form I see the validator being executed, I can see the errors being added and even I'm printing the errors out to see if they are there (they are) but for some reason when the view gets rendered It only sees the model inside the BindingResult but no the errors. Any Ideas?

    Form Controller snippet of the POST method.

    Code:
    @RequestMapping(method = RequestMethod.POST)
        public String processSubmit(@ModelAttribute("category") CategoryModel category, BindingResult result) {
    
            logger.info("saving category: " + category);
    
            new CategoryValidator().validate(category, result);
    		if (result.hasErrors()) {
    			//logger.info(result.toString());
    			//logger.info(category.toString());
    			logger.info(result.toString());
    			Iterator<ObjectError> iter = (result.getAllErrors()).iterator();
    			
    			while(iter.hasNext()) {
    				ObjectError oe = iter.next();
    				logger.info("OBJECT ERROR: CODE -> " + oe.getCode() + " MESSAGE -> " + oe.getCodes());
    			}
    			
    			return "categories/add";
    		} else {
    			categoriesManager.saveCategory(category);
    		}
    View (Velocity):

    Code:
    <html>
      <head>
      	<title>#springMessage("category.add")</title>
      	<style>
        	.error { color: red; }
      	</style>
      </head>
      <body>
        <h1>#springMessage("category.add")</h1>
        <form action="#springUrl('/categories/add')" method="POST">
        	#springFormHiddenInput("category.id" "")
    		Name: #springFormInput("category.name" "") <br/>
    		Key: #springFormInput("category.key" "") <br/>
    		Description: #springFormTextarea("category.description" "") <br/>
    	    <br>
    	    	#springShowErrors("<br>" "error")
    	    <br>
    	    <input type="submit" value="submit"/>
    	    <input type="button" value="Cancel" onclick="document.location='#springUrl('/categories/list')';"/>
        </form>
        <br>
      </body>
    </html>
    And the validator:

    Code:
    public class CategoryValidator implements Validator {
    
        /** Logger for this class and subclasses */
        protected final Log logger = LogFactory.getLog(getClass());
    
        @Override
        public boolean supports(Class clazz) {
            return CategoryModel.class.equals(clazz);
        }
    
        @Override
        public void validate(Object obj, Errors errors) {
        	
        	CategoryModel cm = (CategoryModel) obj;
        	
            if (cm == null) {
            	errors.reject("category.form.error");
            } else {
                logger.info("Validating name");
                if ((cm.getName()).length() <= 0) {
                    errors.rejectValue("name", "category.form.name.error", null, "category.form.name.error");
                }
            }
        }
    Any help will be appreciated.

    Thank you.

  2. #2

    Default

    So finally the solution is (if you read well the view docs):

    "showErrors (simplify display of validation errors for the bound field)" as it says BOUND to a field... so the macro showErrors should be placed NOT at the end like I was doing but on each field or at the beginning for a global error macro.

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •