Question


I am trying to implement a "Save as Draft" functioning in my project using Yii. I have a form with 2 buttons :- Submit and Save . On clicking the Submit button , after validating all the fields including required fields the form data is saved into the database. It works perfect. On clicking Save button I need to save the form data without default validations into the database. How can I implement this disabling of validation in a controller action ??
All advices are acceptable..
Thanks

Était-ce utile?

La solution

Turning off validation rules all together is easy:

$model->save(false);

This will not do any validation and will just try and save your model (may still fail on the database side).

But if you want to run some validation ,you might want to look into Scenarios here. They allow you to specify a different set of rules depending on which scenario you initialize the model with. That way you can only turn on/off whole sets of validation rules.

$model = new Thingy();
$model->save(); // All default validation rules

$model = new Thingy('draft');
$model->save(); //Applies all default & "draft" validation rules

Autres conseils

Your question says in Controller but as far as I know, in controller we have filters which do for example check the permissions. That can be overridden as explained in this section of the guide. If you meant the validation that is done in model, then you can use scenarios (bypass validation by binding rules to scenario and no validation in other scenarios). Check this thread that discuss a like problem

If I have misunderstood your question please comment here so that I update the answer accordingly!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top