Yii CActiveForm验证问题


Yii CActiveForm Validation issue

我很难理解CActiveForm验证,它是如何工作的?这是我的表格:

$form = $this->beginWidget('CActiveForm', array(
    'id' => 'swim-subscribe-form',
    'enableAjaxValidation' => true,
    'action'=>"/mycontroller/myfunction"
        ));
echo $form->labelEx($model, 'companion_tel', array('label' => 'telefon:'));
$this->endWidget();

在我的函数中,我有:

if (isset($_POST)) 
$model->attributes = $_POST;
   if ($model->save()) {
       $this->redirect('/another_controller');
   }
}

在我的模型中:

class myModel extends ActiveRecord {
  public $companion_tel;
  public function rules() {
  return array(
            array('companion_tel' , 'required', 'message'=>'Invalid !'),
  );
}
}

实际上,我想在提交后显示消息Invalid。我该怎么做?

以简单的方式

 if (isset($_POST['YourForm'])) {
            $model->attributes = $_POST['YourForm'];
            if ($model->validate()) {
                if ($model->save()) {
                    $this->redirect('/another_controller');
                }
        }
 }
$this->render('YourViewName', array('model' => $model));

内部视图添加错误

$form = $this->beginWidget('CActiveForm', array(
    'id' => 'swim-subscribe-form',
    'enableAjaxValidation' => true,
    'action'=>"/mycontroller/myfunction"
        ));
 echo $form->error($model,'companion_tel'); //error
echo $form->labelEx($model, 'companion_tel', array('label' => 'telefon:'));
$this->endWidget();