You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Robert Taylor <rt...@dtgresults.com> on 2010/09/23 03:26:38 UTC

additional onsubmit javascript validation

Greetings,

I'm using Struts2.2.1 and have a form using the xhtml theme which performs some simple javascript validation (required, etc...).
Works great.
Now, after the simple javascript validation executes I would like to add some more validation to the onsubmit event.
Apart from modifying a template, I couldn't see how this could be achieved easily.
If you include the validation in the form onsubmit attribute, it is prepended to the dynamically generated javascript validation function name. 

For example:
<s:form action="MyAction" namespace="/mynamespace" theme="xhtml" validate="true" onsubmit="return myValidation()">

produces the following markup.

<form id="Myaction" 
         name="MyAction" 
         onsubmit="return myValidation(); return validateForm_MyAction();" 
         action="/scname/mynamespace/MyAction.html" method="post"  
         onreset="clearErrorMessages(this);clearErrorLabels(this);">

As is evident, this won't work for me. 

I want to leverage the dynamically generated Javascript validation when validate="true". If that validation succeeds, then I want to execute some proprietary validation.

I also know the name of the dynamically generated javascript method so I could call it in myValidation();


Any suggestions would be appreciated.

Thanks,

/robert



Re: additional onsubmit javascript validation

Posted by Robert Taylor <rt...@dtgresults.com>.
Hi Mead,

Binding validation to the click event on the submit button is not what i 
want.
This would fire my validation before the onsubmit validation occurs.

I want to leverage the Struts2 javascript validation framework which already 
exists.
Then, if that succeeds, I would like to fire additional javascript 
validation.
The Struts2 validation framework dynamically generates the appropriate 
validation for
the form if the form has validate="true". Otherwise it doesn't generate it. 
However, when
validate="true", it appends "return validateForm_MyAction();" as an onsubmit 
handler to any existing onsubmit attribute value.

With regards to your second suggestion "removing the onsubmit event". I 
cannot do this (without modifying code/template)
because if validate="true" in the form, it will automatically populate the 
form onsubmit attribute.

If I set validate="false", then the Struts2 validation framework doesn't 
auto-generate the form javascript validation.



/robert


----- Original Message ----- 
From: "Mead Lai" <la...@gmail.com>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Wednesday, September 22, 2010 9:50 PM
Subject: Re: additional onsubmit javascript validation


> Hi Robert,
>
> Do you have another submit button? such as <s:submit/>,
> Try to bind a event listener to this button, that when you click this
> button, you do some validation();
>
> Another way is using the js to remove the  "onsubmit" event, then binding
> another method you write, and invoke the myValidation() in that method;
> bind
> AnoterMethod(){
>
> if(!validateForm_MyAction()){return;}
> //Here do your validation
> return myValidation()
> }
>
> Regards,
> Mead
> 


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


Re: additional onsubmit javascript validation

Posted by Mead Lai <la...@gmail.com>.
Hi Robert,

Do you have another submit button? such as <s:submit/>,
Try to bind a event listener to this button, that when you click this
button, you do some validation();

Another way is using the js to remove the  "onsubmit" event, then binding
another method you write, and invoke the myValidation() in that method;
bind
AnoterMethod(){

if(!validateForm_MyAction()){return;}
//Here do your validation
return myValidation()
}

Regards,
Mead

Re: additional onsubmit javascript validation

Posted by Robert Taylor <rt...@dtgresults.com>.
Hi Dale,

I'm just using the xhtml theme, but I missed that chunk of code in 
xhtml/form-validate.ftl.
That's what I needed.

Thanks,

/robert

----- Original Message ----- 
From: "Dale Newfield" <da...@newfield.org>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Cc: "Robert Taylor" <rt...@dtgresults.com>
Sent: Thursday, September 23, 2010 1:51 PM
Subject: Re: additional onsubmit javascript validation


> On 9/23/10 7:48 AM, Robert Taylor wrote:
>> I'm not sure modifying the templates will work here.
>
> css_xhtml/form-validate.ftl contains:
>
>     <#if parameters.onsubmit??>
>         ${tag.addParameter('onsubmit', "${parameters.onsubmit}; return 
> validateForm_${parameters.id}();")}
>     <#else>
>         ${tag.addParameter('onsubmit', "return 
> validateForm_${parameters.id}();")}
>     </#if>
>
> If you override that file and change one line like so:
>
>     <#if parameters.onsubmit??>
>         ${tag.addParameter('onsubmit', "return 
> (validateForm_${parameters.id}() && ${parameters.onsubmit});")}
>     <#else>
>         ${tag.addParameter('onsubmit', "return 
> validateForm_${parameters.id}();")}
>     </#if>
>
> and make your onsubmit attribute just be javascript that generates a 
> true/false value (FOO) instead of "return FOO", then I think you'll get 
> what you want.
>
> -Dale
>
> ---------------------------------------------------------------------
> 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: additional onsubmit javascript validation

Posted by Dave Newton <da...@gmail.com>.
(And binding an event to onclick or onsubmit doesn't mean that existing
handlers have to go away, depending on how you do it, or if you save
existing handlers.)

On Thu, Sep 23, 2010 at 1:51 PM, Dale Newfield <da...@newfield.org> wrote:

> On 9/23/10 7:48 AM, Robert Taylor wrote:
>
>> I'm not sure modifying the templates will work here.
>>
>
> css_xhtml/form-validate.ftl contains:
>
>    <#if parameters.onsubmit??>
>        ${tag.addParameter('onsubmit', "${parameters.onsubmit}; return
> validateForm_${parameters.id}();")}
>    <#else>
>        ${tag.addParameter('onsubmit', "return validateForm_${parameters.id
> }();")}
>    </#if>
>
> If you override that file and change one line like so:
>
>    <#if parameters.onsubmit??>
>        ${tag.addParameter('onsubmit', "return (validateForm_${
> parameters.id}() && ${parameters.onsubmit});")}
>    <#else>
>        ${tag.addParameter('onsubmit', "return validateForm_${parameters.id
> }();")}
>    </#if>
>
> and make your onsubmit attribute just be javascript that generates a
> true/false value (FOO) instead of "return FOO", then I think you'll get what
> you want.
>
> -Dale
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: additional onsubmit javascript validation

Posted by Dale Newfield <da...@newfield.org>.
On 9/23/10 7:48 AM, Robert Taylor wrote:
> I'm not sure modifying the templates will work here.

css_xhtml/form-validate.ftl contains:

     <#if parameters.onsubmit??>
         ${tag.addParameter('onsubmit', "${parameters.onsubmit}; return 
validateForm_${parameters.id}();")}
     <#else>
         ${tag.addParameter('onsubmit', "return 
validateForm_${parameters.id}();")}
     </#if>

If you override that file and change one line like so:

     <#if parameters.onsubmit??>
         ${tag.addParameter('onsubmit', "return 
(validateForm_${parameters.id}() && ${parameters.onsubmit});")}
     <#else>
         ${tag.addParameter('onsubmit', "return 
validateForm_${parameters.id}();")}
     </#if>

and make your onsubmit attribute just be javascript that generates a 
true/false value (FOO) instead of "return FOO", then I think you'll get 
what you want.

-Dale

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


Re: additional onsubmit javascript validation

Posted by Robert Taylor <rt...@dtgresults.com>.
Hi Dave,

Based on my requirements (leverage the existing Struts2 client-side 
validation framework),
I'm not sure modifying the templates will work here.

It appears that when validate="true" I need a way to tell Struts2 validation 
framework to not
populate the onsubmit attribute. But I still want it to generate the 
appropriate javascript based
on the field validations I have defined for the action. This way I could use 
my own onsubmit handler
to call the auto-generated javascript method (validatForm_MyAction()) at the 
appropriate time.
Reviewing the existing template files didn't reveal that this was possible 
by overriding a template.
Maybe I missed something.


/robert
----- Original Message ----- 
From: "Dave Newton" <da...@gmail.com>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Wednesday, September 22, 2010 9:44 PM
Subject: Re: additional onsubmit javascript validation


>I modified the templates to allow pages to inject their own validation
> messages. Unfortunately I never checked this in. You could do it with a
> JavaScript framework, though, through binding.
>
> Dave
>
> On Wed, Sep 22, 2010 at 9:26 PM, Robert Taylor 
> <rt...@dtgresults.com>wrote:
>
>> Greetings,
>>
>> I'm using Struts2.2.1 and have a form using the xhtml theme which 
>> performs
>> some simple javascript validation (required, etc...).
>> Works great.
>> Now, after the simple javascript validation executes I would like to add
>> some more validation to the onsubmit event.
>> Apart from modifying a template, I couldn't see how this could be 
>> achieved
>> easily.
>> If you include the validation in the form onsubmit attribute, it is
>> prepended to the dynamically generated javascript validation function 
>> name.
>>
>> For example:
>> <s:form action="MyAction" namespace="/mynamespace" theme="xhtml"
>> validate="true" onsubmit="return myValidation()">
>>
>> produces the following markup.
>>
>> <form id="Myaction"
>>         name="MyAction"
>>         onsubmit="return myValidation(); return validateForm_MyAction();"
>>         action="/scname/mynamespace/MyAction.html" method="post"
>>         onreset="clearErrorMessages(this);clearErrorLabels(this);">
>>
>> As is evident, this won't work for me.
>>
>> I want to leverage the dynamically generated Javascript validation when
>> validate="true". If that validation succeeds, then I want to execute some
>> proprietary validation.
>>
>> I also know the name of the dynamically generated javascript method so I
>> could call it in myValidation();
>>
>>
>> Any suggestions would be appreciated.
>>
>> Thanks,
>>
>> /robert
>>
>>
>>
> 


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


Re: additional onsubmit javascript validation

Posted by Dale Newfield <da...@newfield.org>.
On 9/22/10 9:44 PM, Dave Newton wrote:
> I modified the templates

http://struts.apache.org/2.2.1/docs/template-loading.html

describes how you can override existing templates within your application.

-Dale

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


Re: additional onsubmit javascript validation

Posted by Dave Newton <da...@gmail.com>.
I modified the templates to allow pages to inject their own validation
messages. Unfortunately I never checked this in. You could do it with a
JavaScript framework, though, through binding.

Dave

On Wed, Sep 22, 2010 at 9:26 PM, Robert Taylor <rt...@dtgresults.com>wrote:

> Greetings,
>
> I'm using Struts2.2.1 and have a form using the xhtml theme which performs
> some simple javascript validation (required, etc...).
> Works great.
> Now, after the simple javascript validation executes I would like to add
> some more validation to the onsubmit event.
> Apart from modifying a template, I couldn't see how this could be achieved
> easily.
> If you include the validation in the form onsubmit attribute, it is
> prepended to the dynamically generated javascript validation function name.
>
> For example:
> <s:form action="MyAction" namespace="/mynamespace" theme="xhtml"
> validate="true" onsubmit="return myValidation()">
>
> produces the following markup.
>
> <form id="Myaction"
>         name="MyAction"
>         onsubmit="return myValidation(); return validateForm_MyAction();"
>         action="/scname/mynamespace/MyAction.html" method="post"
>         onreset="clearErrorMessages(this);clearErrorLabels(this);">
>
> As is evident, this won't work for me.
>
> I want to leverage the dynamically generated Javascript validation when
> validate="true". If that validation succeeds, then I want to execute some
> proprietary validation.
>
> I also know the name of the dynamically generated javascript method so I
> could call it in myValidation();
>
>
> Any suggestions would be appreciated.
>
> Thanks,
>
> /robert
>
>
>