假设我有model
扩展CActiveRecord
,我使用$this->addError("foo", "Bar is broken");
向该模型添加了一些错误。
现在假设我在浏览器发送AJAX请求时验证我的模型,如下所示:
if(isset($_POST['ajax']) && $_POST['ajax']==='user-login-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
我的问题是:¿如何将错误($model->getErrors()
返回一个数组)发送到浏览器,以便处理来自CActiveForm
的验证错误的Yii代码将接收我的错误并显示它们,必须修改客户端代码吗?
===========编辑==========
登录视图:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'usuarios-login-form',
'enableAjaxValidation'=>true,
)); ?>
<div class="row flush">
<div class="4u">
<?php echo $form->labelEx($model,'correo', array(
'class'=>'pull-right'
)); ?>
</div>
<div class="4u">
<?php echo $form->textField($model,'correo'); ?>
</div>
<div class="4u">
<?php echo $form->error($model,'correo', array(
'class'=>'pull-left alert'
)); ?>
</div>
</div>
<div class="row flush">
<div class="4u">
<?php echo $form->labelEx($model,'password', array(
'class'=>'pull-right'
)); ?>
</div>
<div class="4u">
<?php echo $form->passwordField($model,'password'); ?>
</div>
<div class="4u">
<?php echo $form->error($model,'password', array(
'class'=>'pull-left alert'
)); ?>
</div>
</div>
<div class="row flush">
<div class="4u"> </div>
<div class="8u">
<div class="pull-left infotxt">Los campos con <span class="required">*</span> son obligatorios.</div>
</div>
</div>
<div class="row flush">
<div class="4u"> </div>
<div class="8u">
<?php echo CHtml::submitButton('Iniciar sesion', array(
'class'=>'button alt pull-left loginbtn'
)); ?>
</div>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
控制器:
public function actionLogin()
{
$model=new Usuarios('login');
// if it is ajax validation request
if(isset($_POST['ajax']) && $_POST['ajax']==='usuarios-login-form')
{
echo CActiveForm::validate($model);
echo $model->login()->getErrors();
Yii::app()->end();
}
if(isset($_POST['Usuarios']))
{
$model->attributes=$_POST['Usuarios'];
if($model->validate() && $model->login()){
if(Yii::app()->user->getState('type')=='Admin'){
$this->redirect(array("admin"));
}elseif(Yii::app()->user->getState('type')=='User'){
$this->redirect(array("perfil"));
}
//$this->redirect(Yii::app()->user->returnUrl);
}else{
echo "adadasd";
echo $model->login();
}
}
$this->render('login',array('model'=>$model));
}
Usuarios类中的登录方法:
public function login(){
$identity = new UserIdentity($this->correo, $this->password);
$res = $identity->authenticate();
if($res == UserIdentity::ERROR_NONE){
Yii::app()->user->login($identity, 3600*24*7);
}else if($res == UserIdentity::ERROR_USERNAME_INVALID){
$this->addError("correo", "El usuario no existe");
}else if($res == UserIdentity::ERROR_PASSWORD_INVALID){
$this->addError("password", "La contraseña es incorrecta");
}else{
$this->addError("", "Error desconocido");
}
return $this;
}
答案 0 :(得分:1)
在您的模型中,您可以覆盖beforeValidate()
并在那里检查您的逻辑错误,
protected function beforeValidate()
{
if($this->foo == 'broken') // check your logic
{
$this->addError("foo", "Bar is broken");//error on foo attribute
return false;
}
return parent::beforeValidate(); // keep the chain
}