Yii Framework Forum: jformvalidate onsubmit - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

jformvalidate onsubmit question Rate Topic: -----

#1 User is offline   tombrown 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 17-March 09

Posted 08 September 2009 - 11:48 AM

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
0

#2 User is offline   Raoul 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 647
  • Joined: 29-November 08
  • Location:Paris, France

Posted 09 September 2009 - 05:07 PM

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 :

  • 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 ...
ciao
8)
0

#3 User is offline   tombrown 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 17-March 09

Posted 10 September 2009 - 03:11 AM

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

#4 User is offline   Raoul 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 647
  • Joined: 29-November 08
  • Location:Paris, France

Posted 11 September 2009 - 01:34 PM

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
0

#5 User is offline   tombrown 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 17-March 09

Posted 14 September 2009 - 05:09 AM

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

#6 User is offline   Raoul 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 647
  • Joined: 29-November 08
  • Location:Paris, France

Posted 19 September 2009 - 08:50 AM

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 :(
?>


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)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users