2012-01-12 10 views
2

나는 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. 그러니 어리석은 실수를 용서해주십시오. 도와주세요.

+1

"안 : 나는 이런 식으로 코드를 변경 할

둘째 => 사실 'enableAjaxValidation를' 정확히 어떻게 작동합니까? – Jon

+0

@Jon 사용자 정의 유효성 검사기가 오류를 표시하지 않습니다. 또한 내 브라우저에서 Javascript를 비활성화하면 유효성 검사 오류가 표시되지 않으며 사용자 지정 유효성 검사 이외의 유효성 검사 규칙에도 표시되지 않습니다. – ajaybc

답변

2
public function rules() 
{ 
    // NOTE: you should only define rules for those attributes that 
    // will receive user inputs. 
    public $pwd_repeat; 
    public $old_pwd; 

    return array(
    array('old_pwd,pwd_repeat,pwd','safe'), 
     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->attributes['old_pwd'])); 

    if($record===null){ 
     $this->addError($attribute, 'Invalid password'); 
    } 
} 

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']; 
      $model->save(); 
      $this->render('changepassword',array('model'=>$model)); 
     } 
     else{ 
      $this->render('changepassword',array('model'=>$model)); 
     } 
    } 
} 

여전히 approch은 아니지만 좋은 ... 그러나 먼저 그것을 얻으십시오.

+0

당신의 접근 방식이 좋지 않습니다 bcz 썼습니다. 어떤 사용자가 입력 한 암호를 가지고 있는지 확인하고 있습니다. 다시 설정하는 중 ... 대신에 로그인 한 사용자의 암호를 얻으십시오. 만약 사용자가 입력 한 것과 같으면 다시 설정하십시오. $ this-> $ attribute 또는 $ this-> attributes [ 'old_pwd'] 또는 $ this-> old_pwd –

+0

고맙습니다 바늘. 그러나 이것은 단순한 백엔드 시스템을위한 것이며 관리자는 단 하나입니다. 그래서 암호가 사용자 이름이 아니라 db에 있는지 확인했습니다. – ajaybc

0

이 시도 :

// FOR CHECKING IF THE PASSWORD IS VALID 
public function checkOldPassword($attribute,$params) 
{ 
    $record=Admin::model()->findByAttributes(array('pwd'=>$this->pwd)); 

    if($record===null){ 
     $this->addError($attribute, 'Invalid password'); 
    } 
} 

$ this- 교체> 속성에 $ this-> PWD

+0

고마워요.하지만 그건 효과가 없습니다. – ajaybc

+0

나는 또한 다음을 시도했다. 심지어 그것이 작동하지 않았다. 나는 뭔가 다른 것이 심각하다고 잘못 생각한다. 'public function checkOldPassword ($ attribute, $ params) { $ this-> addError ($ attribute,'잘못된 암호 '); } ' – ajaybc

0

먼저 나는 그 clientValidation 이전 암호를 검사하여 서버 측 유효성 검사 (Ajax 유효성 검사)가 필요하기 때문에 작동하지 않습니다. clientValidation 등 암호 길이 등을 확인하기위한 좋은

그래서이 줄을 넣어 :

public function checkOldPassword($attribute,$params) 
{ 
    $record = $this->find(array(
       'select' => 'old_password', 
       'condition' => 'username=:username', 
       'params' => array(':username' => $username//or user or w/e ur gonna find user), 
        )); 

if($record===null){ 
    $this->addError($attribute, 'Invalid password'); 
} 
else { 

if($this->old_password != $record->old_password) { 

$this->addError('old_password', "Invalid Password"); 
} 
} 
} 
관련 문제