Поведение
Код: Выделить всё
class MyBehavior extends CModelBehavior{
public function beforeValidate($event) {
die('hi, i`m behavior');
return parent::beforeValidate($event);
}
}
Модель
Код: Выделить всё
class ContactForm extends CFormModel
{
public $name;
public $email;
public $subject;
public $body;
public $verifyCode;
public function behaviors() {
return array(
'mybehavior' => array(
'class' => 'MyBehavior',
),
);
}
public function rules()
{
return array(
// name, email, subject and body are required
array('name, email, subject, body', 'required'),
// email has to be a valid email address
array('email', 'email'),
// verifyCode needs to be entered correctly
array('verifyCode', 'captcha', 'allowEmpty'=>!extension_loaded('gd')),
);
}
public function attributeLabels()
{
return array(
'verifyCode'=>'Verification Code',
);
}
}
Деуствие в контроллере SiteController
Код: Выделить всё
public function actionContact()
{
$model=new ContactForm;
if(isset($_POST['ContactForm']))
{
$model->attributes=$_POST['ContactForm'];
//$model->disableBehavior('mybehavior'); //also does not work
$model->disableBehaviors(); //not working
if($model->validate())
{
$headers="From: {$model->email}\r\nReply-To: {$model->email}";
mail(Yii::app()->params['adminEmail'],$model->subject,$model->body,$headers);
Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
$this->refresh();
}
}
$this->render('contact',array('model'=>$model));
}