This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with validation rules for form with multiple submit buttons

 
Fredrik Andersson
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Liju Cherian
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Fredrik Andersson
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
validwhen validation can only be done on server side.
 
Fredrik Andersson
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic