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

  • Add to errors in FormAction

    Have followed another thread(post)... http://forum.springframework.org/showthread.php?t=15895
    ...for validation/submit but beginer here needs a bit more...

    Code:
    // inherits "bindAndValidate" and "setupForm" capabilities from superclass
    public class UserActions extends FormAction {
    
       private UserDao userDao;
    
       public UserActions(UserDao userDao) {
           setFormObjectName("user");
           setFormObjectClass(User.class);
           setFormObjectScope(ScopeType.FLOW);
           this.userDao = userDao;
       }
    
       public Event userOrderInsert(RequestContext context) throws Exception {
          User user = (User)context.getFlowScope().getAttribute("user");
          userDao.insertUser(user);
          return success();
       }
    }
    Easy to stick a try catch arround the insertUser call but if I do catch an exception how can I get at the errors object used to display errors with spring:bind ?

    Really trying to port spring mvc code that uses Springs SimpleFormController

    Code:
    protected ModelAndView onSubmit(Object command,                                BindException errors) throws Exception
    See, you get errors to put your reason of failure in to.

    My actual spring flow ...

    Code:
    <webflow id="operatorFlow" start-state="start">
        
        <action-state id="start">
    	    <action class="web.system.GetOperatorAction" autowire="byType"/>
            <transition on="success" to="viewOperator"/>
            <transition on="error" to="operatorNotFound"/>
        </action-state>
        
        <view-state id="viewOperator" view="operator">
            <transition on="finish" to="finish"/>
            <transition on="edit" to="editOperator"/>
        </view-state>
    
        <view-state id="editOperator" view="operatorEdit">
            <transition on="cancel" to="viewOperator"/>
            <transition on="submit" to="bindAndValidate"/>
        </view-state>
    
        <action-state id="bindAndValidate">
            <action bean="operatorAction" method="bindAndValidate"/>
            <transition on="success" to="submit"/>
            <transition on="error" to="editOperator"/>
        </action-state>
    	
        <action-state id="submit">
            <action bean="operatorAction" method="updateOperator"/>
            <transition on="success" to="viewOperator"/>
            <transition on="error" to="editOperator"/>
        </action-state>
    
    	<view-state id="operatorNotFound" view="operatorNotFound">
            <transition on="ok" to="finish"/>
        </view-state>
    	
        <end-state id="finish" view="operatorsRedirect"/>
    
    </webflow>
    and my action...

    Code:
    public class OperatorAction extends FormAction {
        
    	public SystemManager systemManager;
    	
    	public OperatorAction() {
            setFormObjectName("operator");
            setFormObjectClass(Operator.class);
            setFormObjectScope(ScopeType.FLOW);
            setValidator(new OperatorValidator());
        }
    
        public Event updateOperator(RequestContext context) {
            Operator o = (Operator)context.getFlowScope().getAttribute("operator");        
            try {
    			//systemManager.updateOperator(o);        	
    			//return success();
            	throw new OperatorNotFoundException("FORCEDERROR");        	
    		} catch (OperatorNotFoundException e) {
    			e.printStackTrace();
    			//How to add errors here ?????
    			return error();
    		}        
        }
        
    	public SystemManager getSystemManager() { return systemManager; }
    	public void setSystemManager(SystemManager sm) { systemManager = sm; }
    }
    Hope this post isnt too confusing. Any help appreciated.

    thanks,
    G.
    Last edited by robyn; May 15th, 2006, 07:11 PM.

  • #2
    We have a getFormObject() convenience accessor but no getFormErrors(). We'll add it.

    Keith

    Comment


    • #3
      After some further searching found the answer in posting
      http://<br /> <a href="http://forum...4732</a><br />

      Code:
      public Event updateOperator(RequestContext context) {
              Operator o = (Operator) context.getFlowScope().getAttribute("operator");        
              try {
      		//systemManager.updateOperator(o);        	
      		//return success();
              	throw new OperatorNotFoundException("FORCEDERROR");        	
      	} catch (OperatorNotFoundException e) {
      		e.printStackTrace();
      		//Here's the code...obvious to you all I'm sure
      		FormObjectAccessor foa = new FormObjectAccessor(context);
      		Errors errors = foa.getFormErrors();
      		errors.reject("operator.not.found");
      		//end of example code
      		return error();
      	}        
      }
      Hope no-one has wasted any time...

      cheers,
      G.
      Last edited by robyn; May 15th, 2006, 07:11 PM.

      Comment


      • #4
        G,

        Yes that will work in most cases, but be careful there. You're specifically asking for the form object that is aliased by the alias "#formObject" -- instead of a form object by a specific name. If this code is in your form action, you should really ask for the form object errors instance by name---using the formObjectName property.

        I've added a getFormErrors() helper on FormAction that does this for you. We really dont' want people using FormObjectAccessor if they can help it.

        Keith

        Comment


        • #5
          Thanks Keith,

          Will note to revisit when I upgrade to latest version. I presume you've added to latest cvs.

          As I'm still new Im limiting myself to released versions with the accompanying samples to help, guide etc.

          What release and when do you expect these changes to be released?

          thanks again,
          G.

          Comment


          • #6
            PR5 probably tommorrow.

            Keith

            Comment

            Working...
            X