jformvalidate onsubmit

Hi everyone. jformvalidate has helped me out a treat.

Have used it loads of times now.

Thanks Raule!

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.

Thanks

Tom

Hi tombrown,

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 :

[list=1]

[*]Validate the form

[*]submit the form on validation success

[/list]

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 …

ciao

8)

First let me apologise for misspelling your name Raoul.

Ok, I have two fields which are compared, postcode, and postcode_compare. postcode_compare is hidden and I want a javascript confirm box to appear if the postcodes are different. Then if the user clicks cancel on the confirm box, I want the submit to be cancelled.

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. :)

ok, thanks for your explanation, now I get it.

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.

Anyway, I don’t see any solution that wouldn’t implicate usage of javascript … sorry not to be able to help.

ciao

8)

ps: no worries for misspelling :D

If the user had no JS enabled, I would like to just let the user submit without receiving any warning (as the warning is, sort of, a pleasantry). If the “Save” button is an ordinary button then the form won’t submit at all.

If you see what I mean…

Thanks for your help anyway. :slight_smile:

hi tombrown,

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’.


<script type="text/javascript">

/*<![CDATA[*/	


	jQuery(document).ready(function() {

		$("#ejsvformid1").submit(function(ev){			

			if( confirm('Warning : You\'ve changed your postal code !! are you insane ? Do you really want to proceed ?') == false){

				ev.stopImmediatePropagation();

				return false;				

			}

		});

	});

/*]]>*/

</script>

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 <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />

?>

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…

ciao

8)