• Post Reply Bookmark Topic Watch Topic
  • New Topic

h:commandButton type  RSS feed

 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am a newbie to JSF and I want to add some JavaScript validation code into my JSF page. I wanted to attach a JavaScript function to a command button, which does the validation. So I created a commandButton as follows,



With in the checkValues() function, I check the values and use form.submit() to submit the form to the server. If the validation failes, it will give an alert, and will not submit anything.

However, when the validation failes, it gives the alert, and the page refreshes. When I looked into the HTML generated by JSF, I found that the type of Input tag is set to "submit", not "button".



Shouldn't the type must be "button" instead?
What has gone wrong here ?

(I used the form's 0nsubmit event for validation and it works all right.)

PS : I have used 0nclick and 0nsumbit (stating with a 0-zero) because I am not allowed to post those words exactly as it is. This is not a typo.
[ January 14, 2008: Message edited by: Yohan Liyanage ]
 
Krishna Srinivasan
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this :



Returun true or false from the method. If you return false the form will not be submitted.
 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. But the problem I have is that the button is rendered as a "Submit" button. But I want to have a normal button, which does not submit the page.

Ex. it generates HTML as

<input type="submit".....

where as I expect

<input type="button" .....
 
Ketan Jambhekar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I faced a similar situation. Checked out the ButtonRenderer class which actually renders the HTML for the commandButton. Sadly this class does not consider "button" as a valid type.



Maybe you can create your own custom component to render the same.

Thanks.
 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I see... I was wondering what went wrong. Thanks Ketan!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!