나는 Yii에게 새로운입니다. 사용자를 위해 비밀번호 페이지를 변경하려고합니다. 나는 유효성 검사를받을 때 문제가있다.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' 이제 문제는 사용자 지정 유효성 검사가 작동하지 않는다는 것입니다.. 또한 자바 스크립트를 끄면 서버 쪽 유효성 검사 오류도 표시되지 않습니다.. 새로운 Yii. 그러니 어리석은 실수를 용서해주십시오. 도와주세요.
"안 : 나는 이런 식으로 코드를 변경 할
둘째 => 사실 'enableAjaxValidation를' 정확히 어떻게 작동합니까? – Jon
@Jon 사용자 정의 유효성 검사기가 오류를 표시하지 않습니다. 또한 내 브라우저에서 Javascript를 비활성화하면 유효성 검사 오류가 표시되지 않으며 사용자 지정 유효성 검사 이외의 유효성 검사 규칙에도 표시되지 않습니다. – ajaybc