File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Help with validation rules for form with multiple submit buttons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Help with validation rules for form with multiple submit buttons" Watch "Help with validation rules for form with multiple submit buttons" New topic
Author

Help with validation rules for form with multiple submit buttons

Fredrik Andersson
Ranch Hand

Joined: May 03, 2005
Posts: 35
Hello! I'm struggling with a form developed with the struts validation framework. To give you an idea of how the form looks like please take a look below:



The form contains:
1 selectlist called productselectlist
1 textfeild called mobilephonenumber
4 submit buttons called submitbutton with 4 different values (Add product, Remove product, Update product, Select product).

If the user pushes "Add product" then mobilephonenumber needs to be (required, minlength, maxlength, mask)

If the user pushes "Remove product" then productselectlist must be selected.

If the user pushes "Select product" then productselectlist must be selected.

If the user pushes "Update product" then productselectlist must be selected and mobilephonenumber needs to be (required, minlength, maxlength, mask).


Unfortunately I do not mange to set up the validation rules for this for the clientside so if some one could give me a hand with this it would be great. Below is my attempt to set up the rules. But for eg. if I push the "Remove product" the "Add product" - rule executes.



Best regards Fredrik


Best regards<br />Fredrik
Liju Cherian
Ranch Hand

Joined: Mar 17, 2006
Posts: 44
I've modified your code from validation.xml file. Try this out.

<formset>
<form name="ProductForm">
<field property="mobilephonenumber" depends="validwhen,minlength,maxlength,mask">
<arg key="ProductForm.clientsidevalidation.mobilephonenumber"/>
<var>
<var-name>test</var-name>
<var-value>((submitbutton!="Add product" && submitbutton!="Update product") or (*this* != null))</var-value>
</var>
<var>
<var-name>minlength</var-name>
<var-value>5</var-value>
</var>
<var>
<var-name>maxlength</var-name>
<var-value>50</var-value>
</var>
<var>
<var-name>mask</var-name>
<var-value>^[0-9]*$</var-value>
</var>
</field>
<field property="productselectlist" depends="validwhen">
<arg key="ProductForm.clientsidevalidation.productselectlist" />
<var>
<var-name>test</var-name>
<var-value>((submitbutton!="Remove product" && submitbutton!="Select product") or (*this* != null))</var-value>
</var>
</field>
</form>
</formset>


Hope this helps. If you still have doubts, check this link out. (Topic - Designing Complex Validations with validwhen)


Liju
[ April 06, 2006: Message edited by: Liju Cherian ]

"What's in a name? That which we call a rose by any other name would smell just as sweet."<br />(William Shakespeare, Romeo and Juliet, Act II, Scene II)
Fredrik Andersson
Ranch Hand

Joined: May 03, 2005
Posts: 35
Hello Liju!

I have struggled all weekend with this but I have not get it to work.

I do may think that there was some errors in the code depending on me. I guess it how ever sholud look like:


But it seems like the validwhen never fires.

If I check the output of the JS-code it look like for eg:



Do you see any thing wrong?

BTW do you think the ?

Best regards
Fredrik
Liju Cherian
Ranch Hand

Joined: Mar 17, 2006
Posts: 44
To be frank I'm not all that great with validwhen validation. By the way did you check out the link I mentioned in my previous post...?? In that
<var-name>test</var-name>
is used instead of
<var-name>validwhen</var-name>
. I too have used it this manner. I have not checked it out in your particular situation though. Let me work on a test application with your situation; i'll let you know the result asap. By the way is ur validation firing at all..?? or is it just the validwhen case that is not firing..???
Fredrik Andersson
Ranch Hand

Joined: May 03, 2005
Posts: 35
Hello!

Thanks for your reply!

Yes I would say that the other validation is fired in this form. (minlength, maxlength and mask) when I push the buttons "Add product" or "Update product". But if the field mobilephonenumber is empty then and I push those buttons I just catch the "error" on the serverside.

Correct me if I'm wrong that there should be a poup saying something when this happens or have I missunderstod the use of validwhen?

Btw I have studying the page you refered to before. Thats tha page I got inspired by.

So if you have any suggestions please let me know.

I will now try to create a Hello World page with the generated JS-code and see if it is possible to get it to work.

Best regards
Fredrik
yin jiang
Greenhorn

Joined: Mar 28, 2006
Posts: 7
validwhen validation can only be done on server side.


My English is poor. Sigh!!!
Fredrik Andersson
Ranch Hand

Joined: May 03, 2005
Posts: 35
Hello!

That would explain it all!!

But are you absolutely sure?

...Since I do specify it in the validation.xml like


I guess the var-name "test" has something to do with the validwhen-rule?

This also gets added to the JS-code on the client like:


To me it looks like the expression in the <var-value> is the JS-code that vill be fired and checked for the validwhen-rule?

But perhaps I have missunderstood the meaning of how to use the validwhen??

Please comment as much as you want.

Best regards
Fredrik
Liju Cherian
Ranch Hand

Joined: Mar 17, 2006
Posts: 44
I'm not sure if this thread is still active, but anyhow... I tried out your scenario Fredrick, and was successful. I just tried out the server side validations though. I'm enclosing my .jsp and validation.xml file with this.

index.jsp


validation.xml


Hope this helps.
Fredrik Andersson
Ranch Hand

Joined: May 03, 2005
Posts: 35
Hello!

Thanks for your reply!
The thread is very much active.

If I get you right you just get it executed on the serverside?

Is that right?

I need to get it executed on the client with a popup.

Best regards
fredrik
Liju Cherian
Ranch Hand

Joined: Mar 17, 2006
Posts: 44
Yes, I tried out server-side validation only. Tried my luck with client-side validation today, but didn't succeed , as of yet...!! Why don't you use some external javascript, from within your page to validate the condition, instead of opting for struts based client-side validation..???

Anyhow... if you come out with a solution for this, do let me know...


Cheers,
Liju
Fredrik Andersson
Ranch Hand

Joined: May 03, 2005
Posts: 35
Hello!

I didn't solved it like I would like to do but for now I do like this.



In my validation.xml I removed nearly everything like:


I added a my own ugly javascript into my jsp like:


It does not look beautiful but it works. So for now I will go for this.

OBS onclick is changed to onKlick since JavaRanch does not like that in posts.

Best regards
Fredrik
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with validation rules for form with multiple submit buttons
 
Similar Threads
How to check if field exist in validation.xml
Problems in struts 1 indexedListProperty
Validator Framework
problem using validwhen and radio button
custom validator and custom tags