【发布时间】:2012-01-12 14:57:52
【问题描述】:
我是 Yii 的新手。我正在为用户制作更改密码页面。当我进行验证时,我遇到了麻烦。
这是视图:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'changepassword-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'old_pwd'); ?>
<?php echo $form->textField($model,'old_pwd'); ?>
<?php echo $form->error($model,'old_pwd'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'pwd'); ?>
<?php echo $form->textField($model,'pwd'); ?>
<?php echo $form->error($model,'pwd'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'pwd_repeat'); ?>
<?php echo $form->passwordField($model, 'pwd_repeat'); ?>
<?php echo $form->error($model,'pwd_repeat'); ?>
<p class="hint">
Passwords must be minimum 6 characters and can contain alphabets, numbers and special characters.
</p>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Change Password'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
型号:
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('user, pwd, status, old_pwd, pwd_repeat', 'required'),
array('user', 'length', 'max'=>50),
array('pwd', 'length', 'max'=>30),
array('status', 'length', 'max'=>10),
array('pwd','compare'),
array('old_pwd','checkOldPassword'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, user, pwd, status', 'safe', 'on'=>'search'),
);
}
// FOR CHECKING IF THE PASSWORD IS VALID
public function checkOldPassword($attribute,$params)
{
$record=Admin::model()->findByAttributes(array('pwd'=>$this->attribute));
if($record===null){
$this->addError($attribute, 'Invalid password');
}
}
控制器:
/**
* Change Password for users
*/
public function actionChangePassword()
{
// renders the view file 'protected/views/site/index.php'
// using the default layout 'protected/views/layouts/main.php'
if(Yii::app()->user->isGuest){
// If the user is guest or not logged in redirect to the login form
$this->redirect(array('site/login'));
}
else{
$model = new Admin;
if(isset($_POST['Admin'])){
$model->attributes=$_POST['Admin'];
$this->render('changepassword',array('model'=>$model));
}
else{
$this->render('changepassword',array('model'=>$model));
}
}
}
我正在使用自定义验证器“checkOldPassword”来检查用户的当前密码。现在的问题是自定义验证不起作用。此外,如果我关闭 javascript 我发现服务器端验证错误也没有显示。我是 Yii 的新手。所以请原谅任何愚蠢的错误。 请帮忙。
【问题讨论】:
-
@Jon 自定义验证器没有显示任何错误。此外,当我在浏览器中禁用 Javascript 时,它不会显示任何验证错误,即使是自定义验证以外的验证规则也不显示。
标签: php validation yii