You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Bryce Fischer <br...@berzerker-soft.com> on 2004/08/26 03:34:34 UTC

ValidatorForm issue

I'm having a problem with a ValidatorForm and validation. The first time 
I try to access the action below, I get a validation error. The form 
never gets the opportunity to be filled in by the Action class (action 
called is /fringe.do?method=edit). Is there any way to tell Struts to 
not do validation unless we are saving? For example, calling 
/fringe.do?method=save? I have solved it by overrideing the validate() 
method, checking the parameters and seeing if "method = 'save'".

I was wondering if this was the only way to accomplish this? I'd like to 
be able to keep that kind of logic out of my form, and put it in the 
validation.xml file, or another configuration file...

Form:

public class FringeForm extends ValidatorForm implements 
java.io.Serializable {
  private String field1;

   public ActionErrors validate(ActionMapping mapping, 
HttpServletRequest request) {
      ActionErrors actionErrors = new ActionErrors();
      String method = request.getParameter("method");
     
      if ("save".equals(method)) {
         actionErrors = super.validate(mapping, request);
      }
     
      return actionErrors;
   }

  // getters and setters omitted for brevity
}

Validation:
<form-validation>
  <!--
    Define global validation config in validation-global.xml
  -->
  <formset>
      <form name="fringeForm">
              <field property="field1" depends="required">
                  <arg0 key="fringeForm.field1"/>
              </field>
      </form>
  </formset>
</form-validation>

And the action mapping looks like this (actual class extends 
DispatchAction):

        <action
            path="/fringe"
            type="org.springframework.web.struts.DelegatingActionProxy"
            name="fringeForm"
            scope="request"
            parameter="method"
            input="/WEB-INF/pages/fringeForm.jsp"
        >
            <forward name="edit" path="/WEB-INF/pages/fringeForm.jsp"/>
            <forward name="success" path="/index.jsp"/>
        </action>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: ValidatorForm issue

Posted by Bryce Fischer <br...@berzerker-soft.com>.
Vic wrote:

>
> I allways say validate = false in mapping, and then manualy call it.
> Would that help?


Thanks for your quick response Vic. Do you mean manually calling it in 
the DispatchAction class (I think that's what you mean, but I didn't 
want to assume)?

> Bryce Fischer wrote:
>
>> I'm having a problem with a ValidatorForm and validation. The first 
>> time I try to access the action below, I get a validation error. The 
>> form never gets the opportunity to be filled in by the Action class 
>> (action called is /fringe.do?method=edit). Is there any way to tell 
>> Struts to not do validation unless we are saving? For example, 
>> calling /fringe.do?method=save? I have solved it by overrideing the 
>> validate() method, checking the parameters and seeing if "method = 
>> 'save'".
>>
>> I was wondering if this was the only way to accomplish this? I'd like 
>> to be able to keep that kind of logic out of my form, and put it in 
>> the validation.xml file, or another configuration file...
>>
>> Form:
>>
>> public class FringeForm extends ValidatorForm implements 
>> java.io.Serializable {
>>  private String field1;
>>
>>   public ActionErrors validate(ActionMapping mapping, 
>> HttpServletRequest request) {
>>      ActionErrors actionErrors = new ActionErrors();
>>      String method = request.getParameter("method");
>>          if ("save".equals(method)) {
>>         actionErrors = super.validate(mapping, request);
>>      }
>>          return actionErrors;
>>   }
>>
>>  // getters and setters omitted for brevity
>> }
>>
>> Validation:
>> <form-validation>
>>  <!--
>>    Define global validation config in validation-global.xml
>>  -->
>>  <formset>
>>      <form name="fringeForm">
>>              <field property="field1" depends="required">
>>                  <arg0 key="fringeForm.field1"/>
>>              </field>
>>      </form>
>>  </formset>
>> </form-validation>
>>
>> And the action mapping looks like this (actual class extends 
>> DispatchAction):
>>
>>        <action
>>            path="/fringe"
>>            type="org.springframework.web.struts.DelegatingActionProxy"
>>            name="fringeForm"
>>            scope="request"
>>            parameter="method"
>>            input="/WEB-INF/pages/fringeForm.jsp"
>>        >
>>            <forward name="edit" path="/WEB-INF/pages/fringeForm.jsp"/>
>>            <forward name="success" path="/index.jsp"/>
>>        </action>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: ValidatorForm issue

Posted by Bryce Fischer <br...@berzerker-soft.com>.
Rick Reumann wrote:

> Vic wrote:
>
>>
>> I allways say validate = false in mapping, and then manualy call it.
>> Would that help?
>
>
> I do the same. Validating manually also gives you the nice advantage 
> of being able to set up any lists or other objects you need in request 
> scope if valdiation fails. Bryce, an example of it might look like:


Thanks. That clarifies what I was thinking. And it makes a lot of sense
(Thanks also to David Friedman, didn't want you to think I didn't 
appreciate your feedback even though I didn't respond to your email).

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: ValidatorForm issue

Posted by Rick Reumann <st...@reumann.net>.
Vic wrote:

> 
> I allways say validate = false in mapping, and then manualy call it.
> Would that help?

I do the same. Validating manually also gives you the nice advantage of 
being able to set up any lists or other objects you need in request 
scope if valdiation fails. Bryce, an example of it might look like:


//class EmployeeDispatchAction – not showing any try/catch stuff

     private void setUp( HttpServletRequest request ) {
         Collection jobCodes = Service.getJobCodes();
         request.setAttribute(“jobCodes”, jobCodes);
     }

     public ActionForward setUpForm(ActionMapping mapping, ...) throws 
Exception {
         setUp( request );
         return (mapping.findForward(UIconstants.TO_FORM));
     }

     public ActionForward update(ActionMapping mapping, ...) throws 
Exception {
         ActionErrors errors = form.validate( mapping, request );
         if ( errors != null && !errors.isEmpty ) {
             saveErrors(request, errors);
             return (mapping.findForward(UIconstants.VALIDATION_FAILURE));
         }
         //Everything OK, continue on...
         //BeanUtils to copy Form to a ValueObject .. call service update
          return (mapping.findForward(UIconstants.SUCCESS));
     }


-- 
Rick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: ValidatorForm issue

Posted by David Friedman <hu...@ix.netcom.com>.
+1 ---- Vic's on target when it comes to validating.  This is an especially
useful tactic when using DispatchActions for CRUD (Create Read Update
Delete) actions.

Regards,
David

-----Original Message-----
From: news [mailto:news@sea.gmane.org]On Behalf Of Vic
Sent: Wednesday, August 25, 2004 9:49 PM
To: user@struts.apache.org
Subject: Re: ValidatorForm issue



I allways say validate = false in mapping, and then manualy call it.
Would that help?
.V

Bryce Fischer wrote:
> I'm having a problem with a ValidatorForm and validation. The first time
> I try to access the action below, I get a validation error. The form
> never gets the opportunity to be filled in by the Action class (action
> called is /fringe.do?method=edit). Is there any way to tell Struts to
> not do validation unless we are saving? For example, calling
> /fringe.do?method=save? I have solved it by overrideing the validate()
> method, checking the parameters and seeing if "method = 'save'".
>
> I was wondering if this was the only way to accomplish this? I'd like to
> be able to keep that kind of logic out of my form, and put it in the
> validation.xml file, or another configuration file...
>
> Form:
>
> public class FringeForm extends ValidatorForm implements
> java.io.Serializable {
>  private String field1;
>
>   public ActionErrors validate(ActionMapping mapping, HttpServletRequest
> request) {
>      ActionErrors actionErrors = new ActionErrors();
>      String method = request.getParameter("method");
>          if ("save".equals(method)) {
>         actionErrors = super.validate(mapping, request);
>      }
>          return actionErrors;
>   }
>
>  // getters and setters omitted for brevity
> }
>
> Validation:
> <form-validation>
>  <!--
>    Define global validation config in validation-global.xml
>  -->
>  <formset>
>      <form name="fringeForm">
>              <field property="field1" depends="required">
>                  <arg0 key="fringeForm.field1"/>
>              </field>
>      </form>
>  </formset>
> </form-validation>
>
> And the action mapping looks like this (actual class extends
> DispatchAction):
>
>        <action
>            path="/fringe"
>            type="org.springframework.web.struts.DelegatingActionProxy"
>            name="fringeForm"
>            scope="request"
>            parameter="method"
>            input="/WEB-INF/pages/fringeForm.jsp"
>        >
>            <forward name="edit" path="/WEB-INF/pages/fringeForm.jsp"/>
>            <forward name="success" path="/index.jsp"/>
>        </action>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: ValidatorForm issue

Posted by Vic <ce...@portalvu.com>.
I allways say validate = false in mapping, and then manualy call it.
Would that help?
.V

Bryce Fischer wrote:
> I'm having a problem with a ValidatorForm and validation. The first time 
> I try to access the action below, I get a validation error. The form 
> never gets the opportunity to be filled in by the Action class (action 
> called is /fringe.do?method=edit). Is there any way to tell Struts to 
> not do validation unless we are saving? For example, calling 
> /fringe.do?method=save? I have solved it by overrideing the validate() 
> method, checking the parameters and seeing if "method = 'save'".
> 
> I was wondering if this was the only way to accomplish this? I'd like to 
> be able to keep that kind of logic out of my form, and put it in the 
> validation.xml file, or another configuration file...
> 
> Form:
> 
> public class FringeForm extends ValidatorForm implements 
> java.io.Serializable {
>  private String field1;
> 
>   public ActionErrors validate(ActionMapping mapping, HttpServletRequest 
> request) {
>      ActionErrors actionErrors = new ActionErrors();
>      String method = request.getParameter("method");
>          if ("save".equals(method)) {
>         actionErrors = super.validate(mapping, request);
>      }
>          return actionErrors;
>   }
> 
>  // getters and setters omitted for brevity
> }
> 
> Validation:
> <form-validation>
>  <!--
>    Define global validation config in validation-global.xml
>  -->
>  <formset>
>      <form name="fringeForm">
>              <field property="field1" depends="required">
>                  <arg0 key="fringeForm.field1"/>
>              </field>
>      </form>
>  </formset>
> </form-validation>
> 
> And the action mapping looks like this (actual class extends 
> DispatchAction):
> 
>        <action
>            path="/fringe"
>            type="org.springframework.web.struts.DelegatingActionProxy"
>            name="fringeForm"
>            scope="request"
>            parameter="method"
>            input="/WEB-INF/pages/fringeForm.jsp"
>        >
>            <forward name="edit" path="/WEB-INF/pages/fringeForm.jsp"/>
>            <forward name="success" path="/index.jsp"/>
>        </action>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: ValidatorForm issue

Posted by Bill Siggelkow <bi...@bellsouth.net>.
While I agree that there is nothing wrong with calling validate() 
yourself; if you are using 1.2 you might want to look into the 
MappingDispatchAction -- it was designed to allow for one Action that 
supports multiple action configurations -- even with different forms, etc.

Bryce Fischer wrote:

> I'm having a problem with a ValidatorForm and validation. The first time 
> I try to access the action below, I get a validation error. The form 
> never gets the opportunity to be filled in by the Action class (action 
> called is /fringe.do?method=edit). Is there any way to tell Struts to 
> not do validation unless we are saving? For example, calling 
> /fringe.do?method=save? I have solved it by overrideing the validate() 
> method, checking the parameters and seeing if "method = 'save'".
> 
> I was wondering if this was the only way to accomplish this? I'd like to 
> be able to keep that kind of logic out of my form, and put it in the 
> validation.xml file, or another configuration file...
> 
> Form:
> 
> public class FringeForm extends ValidatorForm implements 
> java.io.Serializable {
>  private String field1;
> 
>   public ActionErrors validate(ActionMapping mapping, HttpServletRequest 
> request) {
>      ActionErrors actionErrors = new ActionErrors();
>      String method = request.getParameter("method");
>          if ("save".equals(method)) {
>         actionErrors = super.validate(mapping, request);
>      }
>          return actionErrors;
>   }
> 
>  // getters and setters omitted for brevity
> }
> 
> Validation:
> <form-validation>
>  <!--
>    Define global validation config in validation-global.xml
>  -->
>  <formset>
>      <form name="fringeForm">
>              <field property="field1" depends="required">
>                  <arg0 key="fringeForm.field1"/>
>              </field>
>      </form>
>  </formset>
> </form-validation>
> 
> And the action mapping looks like this (actual class extends 
> DispatchAction):
> 
>        <action
>            path="/fringe"
>            type="org.springframework.web.struts.DelegatingActionProxy"
>            name="fringeForm"
>            scope="request"
>            parameter="method"
>            input="/WEB-INF/pages/fringeForm.jsp"
>        >
>            <forward name="edit" path="/WEB-INF/pages/fringeForm.jsp"/>
>            <forward name="success" path="/index.jsp"/>
>        </action>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org