Yii Framework Forum: How to disable client side validation for a [cancel] button? - Yii Framework Forum

Jump to content

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

How to disable client side validation for a [cancel] button? Rate Topic: -----

#1 User is offline   MetaYii 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 393
  • Joined: 07-October 08
  • Location:The Matrix

Posted 20 May 2011 - 10:55 PM

Hi, I'm using yii-1.1.7.r3135. I have a form like this:

<?php
$form = $this->beginWidget('CActiveForm', array(
	'id'=>'recovery-form',
   'enableClientValidation'=>true,
   'focus'=>array($model, 'username'),   
   'errorMessageCssClass'=>'clsErrorMessage',
   'htmlOptions'=>array('autocomplete'=>'off'),
	'clientOptions'=>array(
		'validateOnSubmit'=>true,
  	'errorCssClass'=>'clsError',
  	'successCssClass'=>'clsSuccess',
  	'validatingCssClass'=>'clsValidating',
	),
));
?>   


And then I have these two buttons:

<?php
   echo CHtml::submitButton(Yii::t('Yii', 'Send'), array('name'=>'ok', 'class'=>'clsPrimaryButton'));
   echo CHtml::submitButton(Yii::t('Yii', 'Cancel'), array('name'=>'cancel', 'class'=>'clsSecondaryButton'));
?>


How can I disable client side validation when the Cancel button is pressed? The idea is to redirect to another page when this button is pressed, which I do in the action with this code:

if (isset($_POST['cancel'])) {
	$this->controller->redirect(array('login/'));
}


But since I also need client side-validation, the form is never submitted when Cancel is pressed. Of course this simple reditect could be done with a link, but if the cancelation involves other logic (deleting something in session or user state, for instance) then a link would not work.

Note1: in prado this was done with CausesValidation="false" for the button.
Note2: due to graphic design and management issues, I would prefer not to be forced to use a simple CHtml::link for the "cancel".

Thanks.
Ah! on-off, simplement!
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,005
  • Joined: 16-February 11
  • Location:Japan

Posted 21 May 2011 - 07:06 AM

I'm using 'beforeValidate' clientOption. Like this ...
$form = $this->beginWidget('CActiveForm',
	array(
		'id'=>'holiday-form',
		'enableAjaxValidation' => true,
		'focus' => '#Holiday_year',
		'clientOptions' => array(
			'validateOnSubmit' => true,
			'beforeValidate' => 'js:beforeValidate',
		),
	)
);
$js = "function beforeValidate(form) {
-   if ( form.data('submitObject') {
+   if ( form.data('submitObject') ) {
        this.validateOnSubmit = false;
        this.beforeValidate = '';
        form.submit();
        return false;
    }
    return true;
}";
Yii::app()->clientScript->registerScript('editprofile-form-beforeValidate', $js);


It's working for me anyway, but I don't know whether it's quite good or not, because I couldn't get an answer when I asked a question like yours some time ago. :(
And I'd like my solution to be reviewed by experienced fellows. :P

[EDIT]
The code had an apparent syntax error.
0

#3 User is offline   MetaYii 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 393
  • Joined: 07-October 08
  • Location:The Matrix

Posted 21 May 2011 - 10:27 AM

I missed the beforeValidate option. But I have done this using your idea:

$js = 'function beforeValidate(form) {if ( form.data("submitObject") {this.validateOnSubmit = false;this.beforeValidate = "";form.submit();return false;}return true;}';

$form = $this->beginWidget('CActiveForm', array(
	'id'=>'recovery-form',
   'enableClientValidation'=>true,
   'focus'=>array($model, 'username'),   
   'errorMessageCssClass'=>'clsErrorMessage',
   'htmlOptions'=>array('autocomplete'=>'off'),
	'clientOptions'=>array(
		'validateOnSubmit'=>true,
  	'errorCssClass'=>'clsError',
  	'successCssClass'=>'clsSuccess',
  	'validatingCssClass'=>'clsValidating',
  	'beforeValidate'=>"js:'{$js}'",
	),
));


It seems to work fine, but I feel it too complex. It'd be better if the CActiveForm had an option to indicate which buttons need to have validation and which not.
Ah! on-off, simplement!
0

#4 User is offline   MetaYii 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 393
  • Joined: 07-October 08
  • Location:The Matrix

Posted 25 August 2011 - 09:12 AM

Puf, I did not notice that this causes a js error, because I tested in Firefox. But in MSIE the error is reported with a popup dialog and the form can not be submitted:

http://www.yiiframew...showtopic=22936

Firefox of couse doesn't mind the error and continues submitting the form, which in this case might not be the right behavior.
Ah! on-off, simplement!
0

#5 User is offline   MetaYii 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 393
  • Joined: 07-October 08
  • Location:The Matrix

Posted 25 August 2011 - 09:24 AM

Added a feature request: http://www.yiiframew...submit-buttons/
Ah! on-off, simplement!
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