You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by robinbajaj <ro...@gmail.com> on 2006/11/24 01:24:39 UTC

how to selectively call struts validation from a form

Hi Folks, 
My previous post's subject was not clear. So here's a re-phrase of the
problem.
--------
My loyaltyPAge.jsp has two buttons - Exit and continue.

I want the validation to occur only when "Continue" is clicked upon.
But the <html:form in the jsp points to one actionmapping
having "validate=true" - that's why the Struts validation framework 
tries to validate the input fields regardless of which button is clicked. 

Is there some facility provided by the Struts framework to
SKIP the validation when a specific button (Exit) is clicked
and ONLY OCCUR when another button(*Continue) is clicked . ???

I am using inbuild validation framework (validation-<modulename>.xml
and validator-rules.xml etc.) in this case, 
--------
Thanks for your help,
regards,
robin.
-- 
View this message in context: http://www.nabble.com/how-to-selectively-call-struts-validation-from-a-form-tf2695232.html#a7516473
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: how to selectively call struts validation from a form

Posted by Paul Benedict <pb...@apache.org>.
Robin,

> I only have one issue with this approach. I dont know how to make use of 
> the built-in validation rules provided in validation-rules.xml in my 
> .java code.

You are not trying to replace the validator. You are still going to use 
it, so code in your normal rules in the XML file. All you're doing is 
providing an alternate key (if necessary), and invoking the validator 
yourself. Notice in the example it didn't do anything like checking for 
nulls; it called form.validate() which delegates to the validator.

Paul

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


Re: how to selectively call struts validation from a form

Posted by robin bajaj <ro...@gmail.com>.
Thanks Paul for your input on this issue and
Thanks Wendy for suggesting the following link.

 > Rick has an article on calling validation manually, here:
 >   http://learntechnology.net/validate-manually.do
 >

I understand this stuff
--------
ActionErrors errors = form.validate( mapping, request );
         if ( errors != null && !errors.isEmpty() ) {
             saveErrors(request, errors);
             setUp(request);
             return (mapping.findForward(UIconstants.VALIDATION_FAILURE));
         }
----------
I only have one issue with this approach. I dont know how to make use of 
the built-in validation rules provided in validation-rules.xml in my 
.java code.
I can check for simple things like not-null, not-empty etc. but checking
something like "email" field will be easier if I could leverage the 
rules provided by Struts validation framework in validator-rules.xml.

Is there a way to do this ?
Essentially I want to use validation rules like "email", "mask" from 
validator-rules.xml while writing my Action or ActionForm java code.



Any pointers ?
regards,
robin.




Wendy Smoak wrote:
> On 11/23/06, robinbajaj <ro...@gmail.com> wrote:
>>
>> Hi Folks,
>> My previous post's subject was not clear. So here's a re-phrase of the
>> problem.
>> --------
>> My loyaltyPAge.jsp has two buttons - Exit and continue.
>>
>> I want the validation to occur only when "Continue" is clicked upon.
>> But the <html:form in the jsp points to one actionmapping
>> having "validate=true" - that's why the Struts validation framework
>> tries to validate the input fields regardless of which button is clicked.
> 
> Rick has an article on calling validation manually, here:
>   http://learntechnology.net/validate-manually.do
> 

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


Re: how to selectively call struts validation from a form

Posted by Wendy Smoak <ws...@gmail.com>.
On 11/23/06, robinbajaj <ro...@gmail.com> wrote:
>
> Hi Folks,
> My previous post's subject was not clear. So here's a re-phrase of the
> problem.
> --------
> My loyaltyPAge.jsp has two buttons - Exit and continue.
>
> I want the validation to occur only when "Continue" is clicked upon.
> But the <html:form in the jsp points to one actionmapping
> having "validate=true" - that's why the Struts validation framework
> tries to validate the input fields regardless of which button is clicked.

Rick has an article on calling validation manually, here:
   http://learntechnology.net/validate-manually.do

-- 
Wendy

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


Re: how to selectively call struts validation from a form

Posted by Paul Benedict <pb...@apache.org>.
WongTseng wrote:
> You say the validator is based on whatever key I supply.Is that true, as
> far as I know the validation is linked with the form by the form's name.
ValidatorForm and ValidatorActionForm are coded to select the keys for 
you. The former picks the form name, the second picks the action URI. 
You're totally free to override the getValidationKey() method of the 
ActionForm and provide your own key.

Paul

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


Re: how to selectively call struts validation from a form

Posted by WongTseng <wa...@gmail.com>.
hi,Paul:
    You say the validator is based on whatever key I supply.Is that true, as
far as I know the validation is linked with the form by the form's name.
2006/11/24, Paul Benedict <pb...@apache.org>:
>
> Robin,
>
> The validator is based on whatever key you supply. You can choose the
> key. I like to do "formname(buttonname)" style. So if I have a form
> called "personForm" and a button called "save", my form key in my
> validaton files is "personForm(buttonname)".
>
> Paul
>
>

-- 
Wong Tseng

Re: how to selectively call struts validation from a form

Posted by Paul Benedict <pb...@apache.org>.
Robin,

Yes, there is a way, but Struts cannot automatically supply the solution 
for you. The stumbling block is that you have automatic validation 
turned on. If you only want to validate on a certain button, first turn 
automatic validation off. This will prevent Struts from running the 
validator whenever the request is submitted.

Then inside your Action, you'll have to explicitly determine when it is 
the appropriate time to run the validation. If you're using the new 
Struts EventAction(Dispatcher) classes, you can associate certain button 
s to different methods. Inside the intended action method, you then 
should invoke the validator based on whatever key you determine.

The validator is based on whatever key you supply. You can choose the 
key. I like to do "formname(buttonname)" style. So if I have a form 
called "personForm" and a button called "save", my form key in my 
validaton files is "personForm(buttonname)".

Paul

robinbajaj wrote:
> Hi Folks, 
> My previous post's subject was not clear. So here's a re-phrase of the
> problem.
> --------
> My loyaltyPAge.jsp has two buttons - Exit and continue.
> 
> I want the validation to occur only when "Continue" is clicked upon.
> But the <html:form in the jsp points to one actionmapping
> having "validate=true" - that's why the Struts validation framework 
> tries to validate the input fields regardless of which button is clicked. 
> 
> Is there some facility provided by the Struts framework to
> SKIP the validation when a specific button (Exit) is clicked
> and ONLY OCCUR when another button(*Continue) is clicked . ???
> 
> I am using inbuild validation framework (validation-<modulename>.xml
> and validator-rules.xml etc.) in this case, 
> --------
> Thanks for your help,
> regards,
> robin.

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