You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Gawain Hammond <ga...@192.com> on 2008/09/09 19:39:10 UTC
ajax validation issue
I've started using struts 2.1.2 as I was having some issues after
enabling client side javascript validation on forms (some page requests
hang, and clicking back takes you to the page you're supposed to be on).
I decided to use client side validation to avoid the problems of
populating form elements after validation fails (I'm using the action
tag to populate select drop-downs which I can't get to work after
validation errors return the page to the form).
Since moving to struts 2.1.2, I can't get client side validation to work
properly (some rules are skipped, but are then validated if the form
submits), and it only triggers after clicking the submit button (rather
than from the onblur event as in 2.0.11.x).
So I decided to try the ajax validation (seen on this page):
http://struts.apache.org/2.1.2/docs/ajax-validation.html
I've nearly done it, but now when I press submit I just get a browser
download of the json error fields, like so:
---
/* { "fieldErrors": {"daoDto.name":["Name must be in the format:
[A-Za-z0-9-_]*"],"daoDto.settings":["You must enter a value for
Settings"]}} */
---
I've scoured over the tutorial for what I might have done wrong, and I'm
not too hot with javascript, so I'm left a little confused.
I notice the jsp in the tutorial has an <s:url id="url"
namespace="/validation"../> tag which doesn't get used. Is there
supposed to be a different action for the ajax request than for the form
submission?
I've set a hidden field in the form to set
struts.enableJSONValidation=true. Where else would I put this? I take it
this is making the submit to the action return the plain text json
notation?
Any Ideas?
----------------------------------------------------------
This transmission is strictly confidential, possibly legally privileged, and intended solely for the
addressee. Any views or opinions expressed within it are those of the author and do not necessarily
represent those of 192.com, i-CD Publishing (UK) Ltd or any of it's subsidiary companies. If you
are not the intended recipient then you must not disclose, copy or take any action in reliance of this
transmission. If you have received this transmission in error, please notify the sender as soon as
possible. No employee or agent is authorised to conclude any binding agreement on behalf of
i-CD Publishing (UK) Ltd with another party by email without express written confirmation by an
authorised employee of the Company. http://www.192.com (Tel: 08000 192 192). i-CD Publishing (UK) Ltd
is incorporated in England and Wales, company number 3148549, VAT No. GB 673128728.
RE: ajax validation issue
Posted by Dave Newton <ne...@yahoo.com>.
--- On Thu, 9/11/08, Gawain Hammond wrote:
> 1.) validation.js was missing in my html, I wasn't
> using <sx:head> which seems to be the only tag that
> generates a src path to it, I was using <s:head>
> which afaik was my only choice in 2.0.11.1.
> Upgrading to 2.1.2 resolved this.
>
> I had to put this in manually:
>
> <script type="text/javascript"
> src="/myapp/struts/xhtml/validation.js">
> </script>
In S2.0 the validation.js file should be loaded by the form tag, in form.ftl--it's a little weird that it wouldn't be.
Dave
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
RE: ajax validation issue
Posted by Gawain Hammond <ga...@192.com>.
Right, that has been an interesting romp around in struts.
Jeromy, thanks for pointing me in the right direction and making me
think. I went back to basics, bit the bullet and improved my javascript
jedi abilities by installing firebug. That has really helped.
If this might help anyone else one day in the future, here's what
happened..
It turns out my client validation was NOT working quite right. There
were two issues:
1.) validation.js was missing in my html, I wasn't using <sx:head> which
seems to be the only tag that generates a src path to it, I was using
<s:head> which afaik was my only choice in 2.0.11.1. Upgrading to 2.1.2
resolved this.
I had to put this in manually:
<script type="text/javascript" src="/myapp/struts/xhtml/validation.js">
</script>
2.) I discovered that some of my validation regex was treated slightly
differently in js and in java (I had to mess around with grep on the
command line to experiment a bit, but grep regex worked the same as js
regex), thus meaning that some validation rules were never caught
client-side and were only being caught server-side, hence the validation
inconsistency I encountered. The server side validation then led to my
form element population problem on the next page after the initial form.
With client side validation working well I can use the action tags to
populate form elements as much as I want again.
So, does anyone have any idea why using an action won't populate form
elements after validation returns the form input? (the listAll method is
never invoked in the example below)
Like so:
> <s:action namespace="/admin" name="crud.Group.listAll"
id="groupAction"/>
>
> ...
>
> <s:select label="Group" name="user.group.id" value="%{user.group.id}"
> list="%{#groupAction.list}" listKey="id" listValue="name"/>
Many thanks for your assistance.
----------------------------------------------------------
This transmission is strictly confidential, possibly legally privileged, and intended solely for the
addressee. Any views or opinions expressed within it are those of the author and do not necessarily
represent those of 192.com, i-CD Publishing (UK) Ltd or any of it's subsidiary companies. If you
are not the intended recipient then you must not disclose, copy or take any action in reliance of this
transmission. If you have received this transmission in error, please notify the sender as soon as
possible. No employee or agent is authorised to conclude any binding agreement on behalf of
i-CD Publishing (UK) Ltd with another party by email without express written confirmation by an
authorised employee of the Company. http://www.192.com (Tel: 08000 192 192). i-CD Publishing (UK) Ltd
is incorporated in England and Wales, company number 3148549, VAT No. GB 673128728.
RE: ajax validation issue
Posted by Gawain Hammond <ga...@192.com>.
> Can you confirm whether "crud.Group.listAll" is invoked after
> a validation failure?
No, when validation returns to the form action 'crud.Group.listAll' is
not invoked.
This happens with struts versions 2.0.11.x and 2.1.x
So you would expect using an action tag in this manner (to produce a
list for a select box) to work after validation returns to the form?
Interesting, I may investigate this further as it would be the more
simple solution as I'm a bit over my head with the ajax stuff.
-----Original Message-----
From: Jeromy Evans [mailto:jeromy.evans@blueskyminds.com.au]
Sent: 10 September 2008 13:00
To: Struts Users Mailing List
Subject: Re: ajax validation issue
Gawain Hammond wrote:
> A really nice solution so far has been to an action tag to populate
> forms, which has worked nicely the rest of the time. I've really liked
> using this approach to fill drop-downs, like so:
>
> <s:action namespace="/admin" name="crud.Group.listAll"
> id="groupAction"/>
>
> ...
>
> <s:select label="Group" name="user.group.id" value="%{user.group.id}"
> list="%{#groupAction.list}" listKey="id" listValue="name"/>
>
>
That's a novel approach. I like it.
> Using %{#groupAcation.list} to populate the drop down works great, but
> the only work around I've seen to fill the select box after validation
> returns to the form is rather messy:
>
>
http://struts.apache.org/2.1.2/docs/how-do-we-repopulate-controls-when-v
> alidation-fails.html
>
>
I don't see why the above approach wouldn't be working after a
validation failure. When the INPUT result is returned it presumably
renders the same JSP (and doesn't do a redirect). The problem occurs
when the context/valuestack doesn't contain something you're expecting
(such as the list for the select).
Can you confirm whether "crud.Group.listAll" is invoked after a
validation failure?
> Knowing ajax validation will only work with xhtml/css_html theme is
> helpful, I've been using ajax all along which may explain some issues.
>
That should read client-side validation.
> You said to use the default interceptor stack for 2.1.2, and the
> documentation I read says to use 'jsonValidationWorkflowStack' do I
need
> both of them or will just the default stack work?
>
>
jsonValidationWorkflowStack is correct one.
The autocompleter works because it performs a separate request to
populate the permitted values. Your case is just that the list for the
selects is empty or null.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
----------------------------------------------------------
This transmission is strictly confidential, possibly legally privileged, and intended solely for the
addressee. Any views or opinions expressed within it are those of the author and do not necessarily
represent those of 192.com, i-CD Publishing (UK) Ltd or any of it's subsidiary companies. If you
are not the intended recipient then you must not disclose, copy or take any action in reliance of this
transmission. If you have received this transmission in error, please notify the sender as soon as
possible. No employee or agent is authorised to conclude any binding agreement on behalf of
i-CD Publishing (UK) Ltd with another party by email without express written confirmation by an
authorised employee of the Company. http://www.192.com (Tel: 08000 192 192). i-CD Publishing (UK) Ltd
is incorporated in England and Wales, company number 3148549, VAT No. GB 673128728.
Re: ajax validation issue
Posted by Jeromy Evans <je...@blueskyminds.com.au>.
Gawain Hammond wrote:
> A really nice solution so far has been to an action tag to populate
> forms, which has worked nicely the rest of the time. I've really liked
> using this approach to fill drop-downs, like so:
>
> <s:action namespace="/admin" name="crud.Group.listAll"
> id="groupAction"/>
>
> ...
>
> <s:select label="Group" name="user.group.id" value="%{user.group.id}"
> list="%{#groupAction.list}" listKey="id" listValue="name"/>
>
>
That's a novel approach. I like it.
> Using %{#groupAcation.list} to populate the drop down works great, but
> the only work around I've seen to fill the select box after validation
> returns to the form is rather messy:
>
> http://struts.apache.org/2.1.2/docs/how-do-we-repopulate-controls-when-v
> alidation-fails.html
>
>
I don't see why the above approach wouldn't be working after a
validation failure. When the INPUT result is returned it presumably
renders the same JSP (and doesn't do a redirect). The problem occurs
when the context/valuestack doesn't contain something you're expecting
(such as the list for the select).
Can you confirm whether "crud.Group.listAll" is invoked after a
validation failure?
> Knowing ajax validation will only work with xhtml/css_html theme is
> helpful, I've been using ajax all along which may explain some issues.
>
That should read client-side validation.
> You said to use the default interceptor stack for 2.1.2, and the
> documentation I read says to use 'jsonValidationWorkflowStack' do I need
> both of them or will just the default stack work?
>
>
jsonValidationWorkflowStack is correct one.
The autocompleter works because it performs a separate request to
populate the permitted values. Your case is just that the list for the
selects is empty or null.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
RE: ajax validation issue
Posted by Gawain Hammond <ga...@192.com>.
HI,
Thanks very much for your input. I think a lot of the confusion I'm
having is that I've been following 2.1.x documentation while using
2.0.x. Sometimes it's not been clear if the documentation I'm following
is for a particular version or not, and with the many way to do
something it can be easy to get lost, not that I'm complaining mind you,
I love the flexibility.
I do have basic server-side validation working for forms, but
unfortunately I get the issues with drop-down population after
validation kicks in and returns to my form (validation does work nicely
though :-). I'm really hoping to avoid using prepare as I'm using a
single action to perform crud operations for many classes and my prepare
method would get very large to populate all the drop downs.
A really nice solution so far has been to an action tag to populate
forms, which has worked nicely the rest of the time. I've really liked
using this approach to fill drop-downs, like so:
<s:action namespace="/admin" name="crud.Group.listAll"
id="groupAction"/>
...
<s:select label="Group" name="user.group.id" value="%{user.group.id}"
list="%{#groupAction.list}" listKey="id" listValue="name"/>
Using %{#groupAcation.list} to populate the drop down works great, but
the only work around I've seen to fill the select box after validation
returns to the form is rather messy:
http://struts.apache.org/2.1.2/docs/how-do-we-repopulate-controls-when-v
alidation-fails.html
If I could get the form elements to populate after a failed server-side
validation I'd happily just use server-side validation. I'm not sure why
the above jsp won't populate my drop downs after validation returns the
form, does anyone know if I can I get these action tags to work in such
circumstances?
Knowing ajax validation will only work with xhtml/css_html theme is
helpful, I've been using ajax all along which may explain some issues.
You said to use the default interceptor stack for 2.1.2, and the
documentation I read says to use 'jsonValidationWorkflowStack' do I need
both of them or will just the default stack work?
Many thanks for your time.
Gawain
As a side note, I've used the s:autocomplete form element which does
populate drop down boxes after validation forces the page back to the
form, but there's no similar ajax alternative for multiple select boxes
that I can find, so it's just them that are empty now. I feel so close
and yet so far away from my finishing this :-)
-----Original Message-----
From: Jeromy Evans [mailto:jeromy.evans@blueskyminds.com.au]
Sent: 10 September 2008 01:19
To: Struts Users Mailing List
Subject: Re: ajax validation issue
Gawain Hammond wrote:
>
> ...
>
That's a lot of problems, and to work-around each problem you've moved
to a more sophisticated and complicated solution hoping for the best.
You'll need to get the plain-old-validation working first. If validation
fails, your action's execute method is not invoked, so you need to
ensure all the data needed by the page is available in that case. eg.
If you have a select that needs a list of values, then populate the list
in the prepare method rather than the execute method.
Client-side validation is then generated from your server-side
validation settings. It'll only works with xhtml or css_xhtml theme'd
forms with validate=true. If it fails in individual tags it's possible
there's something wrong with the markup. When you isolate it to certain
fields your browser can help you work this out.
If you want to use ajax validation you'll need to ensure you're using
the default interceptor stack for 2.1.2. It'll use your server-side
validation settings. You'll need the sx:head tag in your page and need
to be attentive to where you use ajax theme'd tags verses other themes
(eg. if you don't want the form posted via ajax).
I suggest you go back to basics; create a new minimal form with a
minimal action and it get it to work.
Hope that helps,
Jeromy Evans
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
----------------------------------------------------------
This transmission is strictly confidential, possibly legally privileged, and intended solely for the
addressee. Any views or opinions expressed within it are those of the author and do not necessarily
represent those of 192.com, i-CD Publishing (UK) Ltd or any of it's subsidiary companies. If you
are not the intended recipient then you must not disclose, copy or take any action in reliance of this
transmission. If you have received this transmission in error, please notify the sender as soon as
possible. No employee or agent is authorised to conclude any binding agreement on behalf of
i-CD Publishing (UK) Ltd with another party by email without express written confirmation by an
authorised employee of the Company. http://www.192.com (Tel: 08000 192 192). i-CD Publishing (UK) Ltd
is incorporated in England and Wales, company number 3148549, VAT No. GB 673128728.
Re: ajax validation issue
Posted by Jeromy Evans <je...@blueskyminds.com.au>.
Gawain Hammond wrote:
>
> ...
>
That's a lot of problems, and to work-around each problem you've moved
to a more sophisticated and complicated solution hoping for the best.
You'll need to get the plain-old-validation working first. If validation
fails, your action's execute method is not invoked, so you need to
ensure all the data needed by the page is available in that case. eg.
If you have a select that needs a list of values, then populate the list
in the prepare method rather than the execute method.
Client-side validation is then generated from your server-side
validation settings. It'll only works with xhtml or css_xhtml theme'd
forms with validate=true. If it fails in individual tags it's possible
there's something wrong with the markup. When you isolate it to certain
fields your browser can help you work this out.
If you want to use ajax validation you'll need to ensure you're using
the default interceptor stack for 2.1.2. It'll use your server-side
validation settings. You'll need the sx:head tag in your page and need
to be attentive to where you use ajax theme'd tags verses other themes
(eg. if you don't want the form posted via ajax).
I suggest you go back to basics; create a new minimal form with a
minimal action and it get it to work.
Hope that helps,
Jeromy Evans
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org