• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Struts 2, checking for field errors and then changing the target of a form

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am using Struts 2, and I have a form, that is displayed in an iframe. When submitted the form should, once finished processing, update the parent. This I have achieved through setting target="_parent" in the form tag on that page.

However if there are validation errors, such as a field in the form is not correctly entered. Then I dont wish to update the parent as I would be expecting an error to be returned from the validator.

So firstly is there any way of controlling this kind of behaviour, such as if correctly processed, update parent, if incorrectly processed update self...



Secondly if there is no simple way of achieving this, can I change the form so that if an error is returned it doesnt continue to set the target for the form as the parent. Because I have several nested iframes consecutive errors progress up throuh the various nested iframes destroying/replacing them. I could live with this occuring once, but not subsequent times...

I can check if an error is returned easily enough, but so far have not been able to alter the form tag in a way that would work....

for example what should work is something like this...


However this fails to compile complaining of a missing </s:form> tag, even though one exists.

Similarly if I do the check within the form tag it also fails....



Complaining that the s:form tag is not correctly formatted.

While neither of the two above code fragments are a perfect solution (far from it) they would at least solve the immediate problem. But neither seem to pass the compiler...

Edited: Oops, I am guessing that the first code fragment fails because I am incorrectly nexting the tags, that the </s:form> tag needs to be within the if statement.

However if I do the same thing through a standard JSP code fragment such as:



It still fails...

Thanks

Michael



 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Personally, I think you're going about this all wrong... If validation fails, you need to display the same form again. If it doesn't, you need to do something in a different frame. It's unlikely there's an easy way to do this using plain S2 tags.

Options include:
- using a plain form tag, so you can manipulate its attributes using custom tags
- validating via ajax, so the form will *always* submit successfully
- do everything via ajax and use JavaScript to do the "real work"

I'm sure there are more options beyond that as well.
 
michael lisser
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, maybe I didnt make that quite clear.

When validation fails, I am using the same form. Hence I get back the form, with the validation errors.

However because when I submitted the form I set the "target=_parent" attribute on the form tag, this same form is now being displayed in the parent iframe. This is the correct iframe if the form is processed successfully, and returns a meaningful result. But not so good if it returns an error, as now I have the original form displayed in the parent iframe.

Using a plain form tag, does have some success, but seems to mess up all my other custom tags, as there is no longer any formatting, and as I have a lot of forms I would rather not be rewriting every page etc.

Going via Ajax is probably a better solution, but again not one I wish to progress just yet.

 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Copy the output created by the S2 form tag.
 
michael lisser
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks David,

Unfortunately that was also an approach I already tried. You would think it would work.... but unfortunately it doesnt.

When you change the S2 form tag to a standard form tag the subsequent S2 tags get reformatted/misformatted.

For example... with an S2 form tag...



becomes...



However without the S2 form tag, and using the equivalent standard tag the same code segment becomes...



This kind of thing is repeated throughout the page. Clearly something is in the S2 form tag that is used to transform the subsequent tags, I would suspect the "theme" as there is nothing else in it, but only guessing on that.
reply
    Bookmark Topic Watch Topic
  • New Topic