我是Yii框架的新手,我需要在登录表单"Username cannot be blank"
中显示验证错误消息。现在,我有一个文本字段,我更新了字段,在验证过程中我希望显示一条消息。我怎么能这样做?
控制器
public function actionUpdate($id)
{
$model = $this->loadModel($id);
// set the parameters for the bizRule
$params = array('GroupzSupport'=>$model);
// now check the bizrule for this user
if (!Yii::app()->user->checkAccess('updateSelf', $params) &&
!Yii::app()->user->checkAccess('admin'))
{
throw new CHttpException(403, 'You are not authorized to perform this action');
}
else
{
if(isset($_POST['GroupzSupport']))
{
$password_current=$_POST['GroupzSupport']['password_current'];
$pass=$model->validatePassword($password_current);
$model->attributes=$_POST['GroupzSupport'];
if($pass==1)
{
$model->password = $model->hashPassword($_POST['GroupzSupport']['password_new']);
if($model->save())
$this->redirect(array('/messageTemplate/admin'));
}
else {$errors="Incorrect Current password"; print '<span style="color:red"><b>';
print '</b><b>'.$errors;
print '</b></span>';}
}
$this->render('update',array(
'model'=>$model,
));
}
}
查看
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'password-recovery-reset-password-form',
'enableAjaxValidation'=>false,
)); ?>
<div class="row"><?php
echo $form->labelEx($model,'username');
echo $form->textField($model,'username',array('size'=>45,'maxlength'=>150));
echo $form->error($model,'username');
?></div>
<div class="row">
<?php echo $form->labelEx($model,'current password'); ?>
<?php echo $form->passwordField($model,'password_current',array('size'=>30,'maxlength'=>30)); ?>
<?php echo $form->error($model,'password_current'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'new password'); ?>
<?php echo $form->textField($model,'password_new',array('size'=>30,'maxlength'=>30)); ?>
<?php echo $form->error($model,'password_new'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'confirm new password'); ?>
<?php echo $form->passwordField($model,'password_repeat',array('size'=>30,'maxlength'=>30)); ?>
<?php echo $form->error($model,'password_repeat'); ?>
</div>
<div class="row buttons"><?php
echo CHtml::submitButton('Reset Your Password');
?></div><?php
$this->endWidget(); ?>
</div>
现在我正在顶部显示它。
我想在文本字段中将其显示在登录页面中。 我该怎么做?
答案 0 :(得分:1)
在重定向之前,将消息添加到所需的字段。
在模型验证器中:
$ this-&gt; addError('field_name',“消息错误。”);
或在Controller动作中:
$ model-&gt; addError('field_name',“消息错误。”);