Bypass Specific Validation Rule And Save Data On Button/link Click

Does anyone know how to get a link in your view form that can save the data and bypass validation for a specific rule/ or all rules?

Here’s how I want this:

  1. The user clicks submit.

  2. If a specific validation rule is not met, the user is notified about it and gets a button that can bypass the validation and save the data.

Although I do not understand why you want to skip validation. Also be careful not to open any security holes in your application with this approach.

One solution could be to set a different validation scenario in the case you want no validation. The drawback is that you can’t notify your user but you could instead write a permant warning message somewhere on the page that all inputs are not validated.

should be very straight forward to implement you could either send a request to an action if the validation is failed redirect to a different action and bypass the validation on second action there are more than one way this is the simplest i can think of

Dear Friend

You can do something like this.

We are forking the actions depending on the an attribute is having error or not.

If model has error we are displaying different submit button.




<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'author-form',

	'enableAjaxValidation'=>false,

	'action'=>(!$model->hasErrors('name'))? CHtml::normalizeUrl(array("test/one")):CHtml::normalizeUrl(array("test/two")) 

)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo $form->errorSummary($model); ?>


	<div class="row">

		<?php echo $form->labelEx($model,'name'); ?>

		<?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>64)); ?>

		<?php echo $form->error($model,'name'); ?>

	</div>


	<div class="row buttons">

		<?php 

		if(!$model->hasErrors('name'))

		   echo CHtml::submitButton('Save'); 

		 else   echo CHtml::submitButton('Save with errors!');

		   ?>

	</div>


<?php $this->endWidget(); ?>


</div><!-- form -->



CONTROLLER




public function actionOne()

{

		$model=new Author;

		if(isset($_POST['Author']))

		{

			$model->attributes=$_POST['Author'];

			if($model->save());

				$this->redirect(array('view','id'=>$model->id));

		}


		$this->render('one',array(

			'model'=>$model,

		));

		}

		

	public function actionTwo()

	{





		if(isset($_POST['Author']))

		{

			$model->attributes=$_POST['Author'];

			if($model->save(false));//skipping validation.

			    $this->redirect(array('view','id'=>$model->id));

	       }


		

     }



The only drawback here is that if user finds error and decides against submissin with error. then he has to reset the form

Regards.

Thank you very much for your reply. I have considered scenarios, but they are not what I need, because I always want the validations to take place, but after that, I want to be able to bypass it.

Wow, thank you for your help. I will get back to you with feedback as soon as possible. :D

Hi, thanks for your help.

I’ve tried what you posted, but I get: Error 400 :(

It redirects me to ‘test/one’ so to say, where ‘test/one’ is my own url of course.

Dear Friend

Trying to simulate your scenario i have created a TestController.

The controller has two actions:one and two.

I have used a model Author(table: author)




class TestController extends Controller

{		

		public function actionOne()

		{

			$model=new Author;


		if(isset($_POST['Author']))

		{

			$model->attributes=$_POST['Author'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


		$this->render('one',array(

			'model'=>$model,

		));

	}

		

	public function actionTwo()

	{


		if(isset($_POST['Author']))

		{

			$model->attributes=$_POST['Author'];

			if($model->save(false)) //skip validation...

			   $this->redirect(array('view','id'=>$model->id));

		}


		

      }

}



views/test/one.php




<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'author-form',

	'enableAjaxValidation'=>false,

	'action'=>(!$model->hasErrors('name'))? CHtml::normalizeUrl(array("test/one")):CHtml::normalizeUrl(array("test/two")) 

)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo $form->errorSummary($model); ?>


	<div class="row">

		<?php echo $form->labelEx($model,'name'); ?>

		<?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>64)); ?>

		<?php echo $form->error($model,'name'); ?>

	</div>


	<div class="row buttons">

		<?php 

		if(!$model->hasErrors('name'))

		   echo CHtml::submitButton('Save'); 

		 else   echo CHtml::submitButton('Save with errors!');

		   ?>

	</div>


<?php $this->endWidget(); ?>


</div><!-- form -->






To make it simple I have created only one attribute called "name".

For this controller, there are no access rules.

If you are doing thing in already existing controller, Modify the access rules accordingly.

I am sorry, but it still doesn’t work, and I am trying really hard to understand why… The submit button should have as an action ‘one’ or ‘two’, right? But when I press submit I am just sent to the url ‘test/one’ with a 400 error. How do I make a request to an action( for example two, which should bypass the validation)?

Dear Friend

When user submits the form without error the inputs are saved.

Here "save" button only is visible. here url is "test/one".

If user submits the button with error, the form is not saved. User returns back to form page.

 Now &quot;save&quot; button disappears. &quot;save with error button&quot; appears.If user clicks &quot;the save with errors&quot; button,


 The form is submitted to &quot;test/two&quot;.

Put the actions one and two in access rules.




public function accessRules()

	{

		return array(

			array('allow',  // allow all users to perform 'index' and 'view' actions

				'actions'=>array('index','view','one','two'),

				'users'=>array('*'),

			),

			array('allow', // allow authenticated user to perform 'create' and 'update' actions

				'actions'=>array('create','update'),

				'users'=>array('@'),

			),

			array('allow', // allow admin user to perform 'admin' and 'delete' actions

				'actions'=>array('admin','delete'),

				'users'=>array('admin'),

			),

			array('deny',  // deny all users

				'users'=>array('*'),

			),

		);

	}



Now another approach striked. It also works.

You can remove the action attribute of CActiveForm.

You can add a separate button for Submit with errors with submit event with separate URL.




<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

        'id'=>'author-form',

        'enableAjaxValidation'=>false,  

//action parameter not declared.

)); ?>


        <p class="note">Fields with <span class="required">*</span> are required.</p>


        <?php echo $form->errorSummary($model); ?>


        <div class="row">

                <?php echo $form->labelEx($model,'name'); ?>

                <?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>64)); ?>

                <?php echo $form->error($model,'name'); ?>

        </div>


        <div class="row buttons">

                <?php 

                

               echo CHtml::submitButton('Save');


               if(!$model->hasErrors('name')) 

                   echo CHtml::submitButton('Save with errors!',array(

                       "submit"=>array("test/two") //url declared here for error button

 ));

                   ?>

        </div>


<?php $this->endWidget(); ?>


</div><!-- form -->




Thank you for your help, but using your method I continue to get the bad request error: 400. :(

Dear Friend

I feel sorry about that.

Kindly furnish your _form.

Kindly mention the attribute for which you want to skip validation.

We will work into this.

Regards.