Warning message similar to $this->addError()

Hi all,

I’m using a form that submits and validates using AJAX. Is there a method I can use that is similar to addError() except I only want to display a warning message and not prevent the form submitting?

The flow is:

[list=1]

[*]User enters a number in a form field

[*]The field gets validated by a custom validation function

[*]xxx method returns a warning message - form is still submitable

[/list]

If there is no YII method for this, any ideas on a good way of writing one?

Thanks!

Flash messages should solve your problem

Thanks Kokomo,

I’ve been considering Flash messages but they didn’t seem like the best way to do it. I’ll write some test code and see if it’s close enough. :)

It would be nice to have this feature

Use two scenarios in your controller, the first with all of your necessary rules + the ‘warning’ rules and the second with just the necessary rules. Change the scenario after Ajax validation, so that when the form is actually submitted it’ll get validated using the important rules.

Is this what you were asking?




public function actionCreate()

{

	$model = new MyModel('insertWithWarnings');


	if (($data = Yii::app()->request()->getQuery('MyModel'))!==null)

	{

		$model->setAttributes($data);

		$this->performAjaxValidation($model);

		$model->setScenario('insert');

		if ($model->save())

		{

			//... saved...

		}

		else

		{

			//... error saving...

		}

	}

	$this->render('update', array('model' =>$model));

}



Or if you really want to be safe, just unset the ‘warning’ validators after performing Ajax validation, ie $model->validatorList->removeAt(). That way you won’t mess up the nesting on accident.