You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Raghu Kanchustambham <kr...@gmail.com> on 2006/01/20 15:22:47 UTC

To use form validation in conjunction with validation.xml

Hi,
When I want to retain validation.xml for validation of most of my fields on
the form. However, the rules for one particular field is too complex to be
done with just the validation.xml. I intend to check for it in the
validation form action. What I see is that the action form's validate
function is not getting called.

I believe that this should be supported.

DynaActionForm code: [ Note: This is extending DynaValidatorForm ]
-----------------------------------

public class StudentEnquiryDynaActionForm extends DynaValidatorForm
{
    public ActionErrors validate(ActionMapping actionMapping, ServletRequest
servletRequest)
    {
            System.out.println("**********************************In the
validation!**********************");
            return errors;
   }

}

--------------------------------------
struts-config.xml:

<form-bean name="studentEnquiryForm" type="
com.tuningfork.student.formobject.StudentEnquiryDynaActionForm">
       <form-property name="studentEnquiry" type="
com.tuningfork.student.businessobject.enquiry.StudentEnquiry"/>
       <form-property name="contactAddress" type="
com.tuningfork.student.businessobject.enquiry.StudentAddress"/>
</form-bean>

<action attribute="studentEnquiryForm" name="studentEnquiryForm"
path="/StudentEnquiryPre" scope="request"
           validate="false" type="
com.tuningfork.student.action.StudentEnquiryAction" parameter="operation">
      <forward name="preCreateSuccess" path="/student/StudentEnquiry.jsp"/>
        <forward name="preUpdateSuccess"
path="/student/StudentEnquiry.jsp"/>
        <forward name="viewSuccess" path="/student/StudentEnquiry.jsp"/>
    </action>

      <action attribute="studentEnquiryForm" name="studentEnquiryForm"
path="/StudentEnquiry" scope="request"
                 validate="true" type="
com.tuningfork.student.action.StudentEnquiryAction" parameter="operation"
input="/StudentEnquiryPre.do?operation=validateFailed">
          <forward name="preCreateSuccess"
path="/student/StudentEnquiry.jsp"/>
          <forward name="viewSuccess" path="/student/StudentEnquiry.jsp"/>
          <forward name="createSuccess"
path="/student/EntryConfirmation.jsp"/>
          <forward name="updateSuccess"
path="/student/EntryConfirmation.jsp"/>
          <forward name="createAndEnrollSuccess"
path="/StudentEnrollmentPre.do?operation=preCreate"
redirect="true"/>
      </action>

---------------------------------------

What I do see is that the validation.xml rules are working just fine. It is
just the validate function in the validatorform is not getting called.

What else do I need to do to make this work ?

Regards,
Raghu

Re: To use form validation in conjunction with validation.xml

Posted by Raghu Kanchustambham <kr...@gmail.com>.
Thanks Rick!
super.validate(mapping, request); works just fine for me.
I am able to now use both the validation.xml as well as the ones coded
in my actionform's validate method. :-)

~raghu~


On 1/21/06, Rick Reumann <ri...@gmail.com> wrote:
> How are you certain the validation.xml isn't being called? Log the
> ActionErrors stuff in the try block after the call to
> validator.validate()
>
> Actually I think the easiest is to just validate what you need
> manually and then I think you should just be able to do:
>
> super.validate(mapping, request);
>
> Which should call the base class validate (which will use your validation.xml)
>
> On 1/20/06, Raghu Kanchustambham <kr...@gmail.com> wrote:
> > Thanks Rick! You spotted it correct...It now executes the validate
> > function of the form :-)
> >
> > Now I move onto second step. I cut paste the code that you sent in the
> > action form's validate function. Now the problem is that
> > validation.xml is not having any effect.
> >
> >
> > Just to paste back the exact code I have in my actionform's validate function:
> >
> >        ServletContext application = getServlet().getServletContext();
> >        ActionErrors errors = new ActionErrors();
> >        String parameter = request.getParameter(mapping.getParameter() );
> >        Validator validator = Resources.initValidator( parameter,
> > this, application, request,  errors, page);
> >
> >        try {
> >          validatorResults = validator.validate();
> >        } catch (ValidatorException e) {
> >            System.out.println("***************************Exception!!!!*********************************************");
> >            //log.error(e.getMessage(), e);
> >        }
> >
> > return errors;
> >
> > Note that I am not attempting to make any additional validations here.
> > I just want to call validation.xml validations using this piece of
> > code you have mailed earlier. However, it appears that validation.xml
> > is being ignored.
> >
> > Do I need to do something else to ensure validation.xml is called and
> > so is the code in validationform's validate function?
> >
> > Regards,
> > Raghu
> >
> > ps: I have not changed my action mappings from what I have last posted.
> >
> >
> >
> > On 1/20/06, Rick Reumann <ri...@gmail.com> wrote:
> > > On 1/20/06, Raghu Kanchustambham <kr...@gmail.com> wrote:
> > > > Let me repaste what I think might be relevant for you to take a look at it:
> > > >
> > > >  DynaActionForm code: [ Note: This is extending DynaValidatorForm ]
> > > > -----------------------------------
> > > >
> > > > public class StudentEnquiryDynaActionForm extends DynaValidatorForm
> > > > {
> > > >     public ActionErrors validate(ActionMapping actionMapping, ServletRequest
> > > > servletRequest)
> > > >     {
> > > >             System.out.println("**********************************In the
> > > > validation!**********************");
> > > >             return errors;
> > > >    }
> > > >
> > > > }
> > >
> > > I think you need to override:
> > >
> > > public ActionErrors validate(ActionMapping mapping,
> > >                                 HttpServletRequest request) {
> > >
> > > (Note  HttpServletRequest request and not  ServletRequest )
> > >
> > > ---------------------------------------------------------------------
> > > 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
> >
> >
>
>
> --
> Rick
>
> ---------------------------------------------------------------------
> 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: To use form validation in conjunction with validation.xml

Posted by Rick Reumann <ri...@gmail.com>.
How are you certain the validation.xml isn't being called? Log the
ActionErrors stuff in the try block after the call to
validator.validate()

Actually I think the easiest is to just validate what you need
manually and then I think you should just be able to do:

super.validate(mapping, request);

Which should call the base class validate (which will use your validation.xml)

On 1/20/06, Raghu Kanchustambham <kr...@gmail.com> wrote:
> Thanks Rick! You spotted it correct...It now executes the validate
> function of the form :-)
>
> Now I move onto second step. I cut paste the code that you sent in the
> action form's validate function. Now the problem is that
> validation.xml is not having any effect.
>
>
> Just to paste back the exact code I have in my actionform's validate function:
>
>        ServletContext application = getServlet().getServletContext();
>        ActionErrors errors = new ActionErrors();
>        String parameter = request.getParameter(mapping.getParameter() );
>        Validator validator = Resources.initValidator( parameter,
> this, application, request,  errors, page);
>
>        try {
>          validatorResults = validator.validate();
>        } catch (ValidatorException e) {
>            System.out.println("***************************Exception!!!!*********************************************");
>            //log.error(e.getMessage(), e);
>        }
>
> return errors;
>
> Note that I am not attempting to make any additional validations here.
> I just want to call validation.xml validations using this piece of
> code you have mailed earlier. However, it appears that validation.xml
> is being ignored.
>
> Do I need to do something else to ensure validation.xml is called and
> so is the code in validationform's validate function?
>
> Regards,
> Raghu
>
> ps: I have not changed my action mappings from what I have last posted.
>
>
>
> On 1/20/06, Rick Reumann <ri...@gmail.com> wrote:
> > On 1/20/06, Raghu Kanchustambham <kr...@gmail.com> wrote:
> > > Let me repaste what I think might be relevant for you to take a look at it:
> > >
> > >  DynaActionForm code: [ Note: This is extending DynaValidatorForm ]
> > > -----------------------------------
> > >
> > > public class StudentEnquiryDynaActionForm extends DynaValidatorForm
> > > {
> > >     public ActionErrors validate(ActionMapping actionMapping, ServletRequest
> > > servletRequest)
> > >     {
> > >             System.out.println("**********************************In the
> > > validation!**********************");
> > >             return errors;
> > >    }
> > >
> > > }
> >
> > I think you need to override:
> >
> > public ActionErrors validate(ActionMapping mapping,
> >                                 HttpServletRequest request) {
> >
> > (Note  HttpServletRequest request and not  ServletRequest )
> >
> > ---------------------------------------------------------------------
> > 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
>
>


--
Rick

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


Re: To use form validation in conjunction with validation.xml

Posted by Raghu Kanchustambham <kr...@gmail.com>.
Thanks Rick! You spotted it correct...It now executes the validate
function of the form :-)

Now I move onto second step. I cut paste the code that you sent in the
action form's validate function. Now the problem is that
validation.xml is not having any effect.


Just to paste back the exact code I have in my actionform's validate function:

        ServletContext application = getServlet().getServletContext();
        ActionErrors errors = new ActionErrors();
        String parameter = request.getParameter(mapping.getParameter() );
        Validator validator = Resources.initValidator( parameter,
this, application, request,  errors, page);

        try {
          validatorResults = validator.validate();
        } catch (ValidatorException e) {
            System.out.println("***************************Exception!!!!*********************************************");
            //log.error(e.getMessage(), e);
        }

return errors;

Note that I am not attempting to make any additional validations here.
I just want to call validation.xml validations using this piece of
code you have mailed earlier. However, it appears that validation.xml
is being ignored.

Do I need to do something else to ensure validation.xml is called and
so is the code in validationform's validate function?

Regards,
Raghu

ps: I have not changed my action mappings from what I have last posted.



On 1/20/06, Rick Reumann <ri...@gmail.com> wrote:
> On 1/20/06, Raghu Kanchustambham <kr...@gmail.com> wrote:
> > Let me repaste what I think might be relevant for you to take a look at it:
> >
> >  DynaActionForm code: [ Note: This is extending DynaValidatorForm ]
> > -----------------------------------
> >
> > public class StudentEnquiryDynaActionForm extends DynaValidatorForm
> > {
> >     public ActionErrors validate(ActionMapping actionMapping, ServletRequest
> > servletRequest)
> >     {
> >             System.out.println("**********************************In the
> > validation!**********************");
> >             return errors;
> >    }
> >
> > }
>
> I think you need to override:
>
> public ActionErrors validate(ActionMapping mapping,
>                                 HttpServletRequest request) {
>
> (Note  HttpServletRequest request and not  ServletRequest )
>
> ---------------------------------------------------------------------
> 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: To use form validation in conjunction with validation.xml

Posted by Rick Reumann <ri...@gmail.com>.
On 1/20/06, Raghu Kanchustambham <kr...@gmail.com> wrote:
> Let me repaste what I think might be relevant for you to take a look at it:
>
>  DynaActionForm code: [ Note: This is extending DynaValidatorForm ]
> -----------------------------------
>
> public class StudentEnquiryDynaActionForm extends DynaValidatorForm
> {
>     public ActionErrors validate(ActionMapping actionMapping, ServletRequest
> servletRequest)
>     {
>             System.out.println("**********************************In the
> validation!**********************");
>             return errors;
>    }
>
> }

I think you need to override:

public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {

(Note  HttpServletRequest request and not  ServletRequest )

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


Re: To use form validation in conjunction with validation.xml

Posted by Raghu Kanchustambham <kr...@gmail.com>.
Let me repaste what I think might be relevant for you to take a look at it:

 DynaActionForm code: [ Note: This is extending DynaValidatorForm ]
-----------------------------------

public class StudentEnquiryDynaActionForm extends DynaValidatorForm
{
    public ActionErrors validate(ActionMapping actionMapping, ServletRequest
servletRequest)
    {
            System.out.println("**********************************In the
validation!**********************");
            return errors;
   }

}

--------------------------------------
struts-config.xml:

<form-bean name="studentEnquiryForm" type="
com.tuningfork.student.formobject.StudentEnquiryDynaActionForm">
       <form-property name="studentEnquiry" type="
com.tuningfork.student.businessobject.enquiry.StudentEnquiry "/>
       <form-property name="contactAddress" type="
com.tuningfork.student.businessobject.enquiry.StudentAddress"/>
</form-bean>

<action attribute="studentEnquiryForm" name="studentEnquiryForm"
path="/StudentEnquiryPre" scope="request"
           validate="false" type="
com.tuningfork.student.action.StudentEnquiryAction " parameter="operation">
      <forward name="preCreateSuccess" path="/student/StudentEnquiry.jsp"/>
        <forward name="preUpdateSuccess"
path="/student/StudentEnquiry.jsp"/>
        <forward name="viewSuccess" path="/student/StudentEnquiry.jsp"/>
    </action>

      <action attribute="studentEnquiryForm" name="studentEnquiryForm"
path="/StudentEnquiry" scope="request"
                 validate="true" type="
com.tuningfork.student.action.StudentEnquiryAction " parameter="operation"
input="/StudentEnquiryPre.do?operation=validateFailed">
          <forward name="preCreateSuccess"
path="/student/StudentEnquiry.jsp"/>
          <forward name="viewSuccess" path="/student/StudentEnquiry.jsp"/>
          <forward name="createSuccess"
path="/student/EntryConfirmation.jsp"/>
          <forward name="updateSuccess"
path="/student/EntryConfirmation.jsp"/>
          <forward name="createAndEnrollSuccess"
path="/StudentEnrollmentPre.do?operation=preCreate"
redirect="true"/>
      </action>

---------------------------------------

Yes, I set validate = true. I also have a set of mappings where
validate=false. What I can say for sure is that validations are kicking in
.... the validation.xml validations are all happening fine. The problem is
that my validate function of the action form is not being called (i.e. I
cant see the string "********In the validation!**********" on my console.

And regarding validating manually, I shall keep it in mind for my next
application. Would not want to disturb something that is (or may be was)
 working right now... :-)

~raghu

ps: Your new website looks quite good. :-) Hope to see even more articles
than we had on strutting site.



On 1/20/06, Rick Reumann <ri...@gmail.com> wrote:
>
> What does your action mapping look like? Do you have validate=true set for
> the form?
>
> Mind you, I'm not a fan of using automatic validation:) I prefer to call
> validate manually from my Action class. I explain my reasons here
> http://www.learntechnology.net/validate-manually.do
>
>
>
> On 1/20/06, Raghu Kanchustambham <kr...@gmail.com> wrote:
> >
> > Thanks Rick for the response.
> > My first issue Rick is that my validate function in the form is not
> > getting
> > called at all.
> > I have simply put a "System.out.println" statement there to verify that
> it
> > is getting called.
> >
> > What you have given is definitely useful for me in the second step :-)
> But
> > I
> > need to get the first step correct... that is to make sure my validate
> > function is being called in addition to validation.xml rules.
> >
> > Could you help me with that?
> >
> > Thanks.
> > Raghu
> >
> >
> >
> > On 1/20/06, Rick Reumann <st...@reumann.net> wrote:
> > >
> > > Raghu Kanchustambham wrote the following on 1/20/2006 9:22 AM:
> > > > Hi,
> > > > When I want to retain validation.xml for validation of most of my
> > fields
> > > on
> > > > the form. However, the rules for one particular field is too complex
> > to
> > > be
> > > > done with just the validation.xml. I intend to check for it in the
> > > > validation form action. What I see is that the action form's
> validate
> > > > function is not getting called.
> > >
> > > Here's how I've done it in the past. There might be a better way:
> > >
> > > //in form Validate method...
> > >
> > > ServletContext application = getServlet().getServletContext();
> > >
> > > ActionErrors errors = new ActionErrors();
> > >
> > > String parameter = request.getParameter( mapping.getParameter() );
> > >
> > > Validator validator = Resources.initValidator( parameter, this,
> > > application, request,  errors, page);
> > >
> > > try {
> > >   validatorResults = validator.validate();
> > > } catch (ValidatorException e) {
> > >   log.error(e.getMessage(), e);
> > > }
> > >
> > > //now do you manual validation say on age
> > > String prop = request.getParameter("prop");
> > > if ( //some test on prop) {
> > >     errors.add(ActionMessages.GLOBAL_MESSAGE, new
> > > ActionError("errors.something", "Prop"));
> > > }
> > >
> > > return errors;
> > >
> > > --
> > > Rick
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > > For additional commands, e-mail: user-help@struts.apache.org
> > >
> > >
> >
> >
>
>
> --
> Rick
>
>

Re: To use form validation in conjunction with validation.xml

Posted by Rick Reumann <ri...@gmail.com>.
What does your action mapping look like? Do you have validate=true set for
the form?

Mind you, I'm not a fan of using automatic validation:) I prefer to call
validate manually from my Action class. I explain my reasons here
http://www.learntechnology.net/validate-manually.do



On 1/20/06, Raghu Kanchustambham <kr...@gmail.com> wrote:
>
> Thanks Rick for the response.
> My first issue Rick is that my validate function in the form is not
> getting
> called at all.
> I have simply put a "System.out.println" statement there to verify that it
> is getting called.
>
> What you have given is definitely useful for me in the second step :-) But
> I
> need to get the first step correct... that is to make sure my validate
> function is being called in addition to validation.xml rules.
>
> Could you help me with that?
>
> Thanks.
> Raghu
>
>
>
> On 1/20/06, Rick Reumann <st...@reumann.net> wrote:
> >
> > Raghu Kanchustambham wrote the following on 1/20/2006 9:22 AM:
> > > Hi,
> > > When I want to retain validation.xml for validation of most of my
> fields
> > on
> > > the form. However, the rules for one particular field is too complex
> to
> > be
> > > done with just the validation.xml. I intend to check for it in the
> > > validation form action. What I see is that the action form's validate
> > > function is not getting called.
> >
> > Here's how I've done it in the past. There might be a better way:
> >
> > //in form Validate method...
> >
> > ServletContext application = getServlet().getServletContext();
> >
> > ActionErrors errors = new ActionErrors();
> >
> > String parameter = request.getParameter( mapping.getParameter() );
> >
> > Validator validator = Resources.initValidator( parameter, this,
> > application, request,  errors, page);
> >
> > try {
> >   validatorResults = validator.validate();
> > } catch (ValidatorException e) {
> >   log.error(e.getMessage(), e);
> > }
> >
> > //now do you manual validation say on age
> > String prop = request.getParameter("prop");
> > if ( //some test on prop) {
> >     errors.add(ActionMessages.GLOBAL_MESSAGE, new
> > ActionError("errors.something", "Prop"));
> > }
> >
> > return errors;
> >
> > --
> > Rick
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
>
>


--
Rick

Re: To use form validation in conjunction with validation.xml

Posted by Raghu Kanchustambham <kr...@gmail.com>.
Thanks Rick for the response.
My first issue Rick is that my validate function in the form is not getting
called at all.
I have simply put a "System.out.println" statement there to verify that it
is getting called.

What you have given is definitely useful for me in the second step :-) But I
need to get the first step correct... that is to make sure my validate
function is being called in addition to validation.xml rules.

Could you help me with that?

Thanks.
Raghu



On 1/20/06, Rick Reumann <st...@reumann.net> wrote:
>
> Raghu Kanchustambham wrote the following on 1/20/2006 9:22 AM:
> > Hi,
> > When I want to retain validation.xml for validation of most of my fields
> on
> > the form. However, the rules for one particular field is too complex to
> be
> > done with just the validation.xml. I intend to check for it in the
> > validation form action. What I see is that the action form's validate
> > function is not getting called.
>
> Here's how I've done it in the past. There might be a better way:
>
> //in form Validate method...
>
> ServletContext application = getServlet().getServletContext();
>
> ActionErrors errors = new ActionErrors();
>
> String parameter = request.getParameter( mapping.getParameter() );
>
> Validator validator = Resources.initValidator( parameter, this,
> application, request,  errors, page);
>
> try {
>   validatorResults = validator.validate();
> } catch (ValidatorException e) {
>   log.error(e.getMessage(), e);
> }
>
> //now do you manual validation say on age
> String prop = request.getParameter("prop");
> if ( //some test on prop) {
>     errors.add(ActionMessages.GLOBAL_MESSAGE, new
> ActionError("errors.something", "Prop"));
> }
>
> return errors;
>
> --
> Rick
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: To use form validation in conjunction with validation.xml

Posted by Rick Reumann <st...@reumann.net>.
Raghu Kanchustambham wrote the following on 1/20/2006 9:22 AM:
> Hi,
> When I want to retain validation.xml for validation of most of my fields on
> the form. However, the rules for one particular field is too complex to be
> done with just the validation.xml. I intend to check for it in the
> validation form action. What I see is that the action form's validate
> function is not getting called. 

Here's how I've done it in the past. There might be a better way:

//in form Validate method...

ServletContext application = getServlet().getServletContext();

ActionErrors errors = new ActionErrors();

String parameter = request.getParameter( mapping.getParameter() );

Validator validator = Resources.initValidator( parameter, this, 
application, request,  errors, page);

try {
   validatorResults = validator.validate();
} catch (ValidatorException e) {
   log.error(e.getMessage(), e);
}

//now do you manual validation say on age
String prop = request.getParameter("prop");
if ( //some test on prop) {
     errors.add(ActionMessages.GLOBAL_MESSAGE, new 
ActionError("errors.something", "Prop"));
}

return errors;

-- 
Rick

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