jformvalidate onsubmit question
Posted 08 September 2009 - 11:48 AM
Have used it loads of times now.
I have a Q though.
I can't seem to get <form onsubmit="return false" ..... to prevent a form from submitting.
Is this due to jformvalidate?
Is there a nice way round this issue.
Posted 09 September 2009 - 05:07 PM
glad to see that jformvalidate is useful for you ... it's nice to have feeback sometimes
Regarding the issue you're describing, note that when a form is validated on the client side, using jformvalidate, a handler function is binded to the onsubmit event for the form. this handler function does the following :
- Validate the form
- submit the form on validation success
Consequently, the form is submited by the onsubmit handler function, and not by the default onsubmit behaviour: setting onsubmit="return false;" does not prevent the form to be submited.
Now I don't know what the context is, and why this form should not be submitted. Maybe that if you provide me with more info I can help ...
Posted 10 September 2009 - 03:11 AM
So my code looks is something like: onsubmit="return this.postcode == this.postcode_compare || confirm('Changing your postcode will reset your work radius, are you sure you want to continue?')"
Another way I could do this would be to have a normal button instead of a submit button, with the on click attribute containing the code for the check and confirm dialogue, which instead calls form.submit() if the user clicks Ok - but this means that the form won't submit if the user doesn't have JS enabled.
I think my best option may be to remind the user of the effect of changing their postcode before they click any button.
Posted 11 September 2009 - 01:34 PM
In fact everything is clear except when your refering to the fact that some user may have disable JS. In this case, are you saying that something like :
<form onsubmit="return this.postcode == this.postcode_compare || confirm('Changing your postcode will reset your work radius, are you sure you want to continue?')" .... />
..would work ? I thought that this was JS ... but maybe I'm mistaken.
ps: no worries for misspelling
Posted 14 September 2009 - 05:09 AM
If you see what I mean..
Thanks for your help anyway. :-)
Posted 19 September 2009 - 08:50 AM
please don't think I forgot about your problem, but I have been overloaded with work recently (poor me ).
I'm afraid that there is no built-in feature in the jformvalidate extension, dedicated to achieve want you want. However, I think that a small JS script could do the trick. For instance, if you insert the following code after the form, the submition will be canceled if the user select 'no'.
If JS is disabled, then no problem, the form will be submitted. The only thing to take care of is provide the correct form id (here 'ejsvformid1'). The nice way would be to use:
<?php EHtml::getJValidateInstance()->getCurrentFormId(); // won't work :( ?>
But unfortunalty the guy who wrote this extension has defined the getCurrentFormId() method as protected (it will be public in a next release). So you can :
- modify getCurrentFormId() scope in the source code (file EJFValidate.php, line:275)
- hard code it (bhou bhou !)
I hope that this will help you solve your problem...