Announcement Announcement Module
Collapse
No announcement yet.
Bind errors Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Bind errors

    Hi there

    how do I add "bind exceptions" in my OnSubmit method?

    I override
    ModelAndView onSubmit(HttpServletRequest request,
    HttpServletResponse response, Object command, BindException errors)
    and under some circumstances I want to generate an error for a specific field, for showing on the screen - in the same fashion as Spring automatically generates in the <spring:bind> tag.

    Thanks,
    Peter

  • #2
    The usual place is in the validator class associated with the form.

    If you must reject a field in the onSubmit method, try:

    errors.rejectValue(fieldName, errorCode, errorArgs, defaultMessage);

    Comment


    • #3
      No, that won't be sufficient. The errors.rejectValue(..) won't affect the flow. Apparently, once the validation has been done, the errors aren't looked after any more. We subclassed SimpleFormController and overrided processFormSubmission into:

      Code:
          protected ModelAndView processFormSubmission&#40;
                  HttpServletRequest request, HttpServletResponse response, Object command, BindException errors&#41;
                  throws Exception &#123;
              if &#40;errors.hasErrors&#40;&#41; || isFormChangeRequest&#40;request&#41;&#41; &#123;
                  if &#40;logger.isDebugEnabled&#40;&#41;&#41; &#123;
                      logger.debug&#40;"Data binding errors&#58; " + errors.getErrorCount&#40;&#41;&#41;;
                  &#125;
                  return showForm&#40;request, response, errors&#41;;
              &#125;
              else &#123;
                  logger.debug&#40;"No errors -> processing submit"&#41;;
                  ModelAndView modelAndView = onSubmit&#40;request, response, command, errors&#41;;
                  // New code&#58; look if the onSubmit has errors
                  if &#40;!errors.hasErrors&#40;&#41;&#41; &#123;
                      return modelAndView;
                  &#125; else &#123;
                      if &#40;logger.isDebugEnabled&#40;&#41;&#41; &#123;
                          logger.debug&#40;"onSubmit errors&#58; " + errors.getErrorCount&#40;&#41;&#41;;
                      &#125;
                      return showForm&#40;request, response, errors&#41;;
                  &#125;
              &#125;
          &#125;
      Perhaps this should be included in the Spring framework, as business validation can happen in the onSubmit.

      Comment


      • #4
        Strange - I tried this before posting just to check and it works fine for me.

        Comment


        • #5
          I ended up (using a SimpleFormController subclass) with the following:

          Code:
          protected ModelAndView onSubmit&#40;HttpServletRequest request, HttpServletResponse response, Object command, BindException errors&#41; throws Exception&#123;
              ...
              if &#40;errorCond&#41; &#123;
                  errors.rejectValue&#40;"fieldName", "code.value", 
                                  new String&#91;&#93; &#123;"Substitution argument 1", "Substitution argument 1"&#125;, "Default error message."&#41;;
                          
                  Map model = errors.getModel&#40;&#41;;
                  model.put&#40;"somethingElse", somethingElse&#41;;
                  return new ModelAndView&#40;this.getFormView&#40;&#41;, model&#41;;
              &#125;
              ...
              return new ModelAndView&#40;this.getSuccessView&#40;&#41;&#41;;
          &#125;
          [/code]

          Comment


          • #6
            It can be done in onSubmit as per the code example before this post, however, instead of doing

            return new ModelAndView(this.getFormView(), model);

            you are generally better off calling showForm(), since if the controller is used with 'sessionForm=true' only the latter will take care of putting the form back into the session. If you just return the ModelAndView with the form view, the form will display fine, but it won't have been put into the session, and when you POST back in, the whole thing will be created from scratch (i.e. there will be a new command object created, as if sessionForm=false was set). Probably not what you want or you would not be using session forms in the first place.

            I would argue that Spring itself could detect that the specific form view is being returned and take care of things...

            Comment

            Working...
            X