How To Prevent Save Action Using Cmodelevent::isvalid?

Hi guys, How to prevent save action using CModelEvent::isValid in beforeSave() method?

isValid? Do you mean don’t validate on save()? You can stop validation checks on save with


$model=>save(false)

in you action.

No, I mean prevent save action in event handler. Please see onBeforeSave() description.


 $event->isValid = false;

Also see isValid property

To use the onBeforeSave() We should override beforeSave() method in Model like bellow:




class Post extends CActiveRecord

{

   public function beforeSave()

   {

      // if something happen prevent saving data

   }

}



in beforeSave() there are no $event variable or parameter!

Further research says that isValid is in the beforeSave of a behavior. I think if you return false, in the model beforeSave, you get the same result. Check the CActiveRecord code in the framework/db/ar dir. The code has documentation above the function declaration.

Yes! It works. Thank you jkofsky.




public function beforeSave()

{

   if($a != $<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='B)' />  // the condition to prevent

   {

      return false;  // prevent saving data

   }

   return parent:: beforeSave();  // allow saving

}



No problem. How 'bout an up vote :huh:

I’m so sorry. I like your comments in all topics. Thank you again. :)

Thanks for your comment and vote B)