2012-01-12 44 views
2

私はYiiにとって新しいです。私はユーザーのパスワード変更ページを作成しています。私が妥当性確認をしたら、私は問題を抱えています。Yiiカスタム検証とサーバー側検証?

これはビューである:私は使用しています

/** 
* 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)); 
     } 
    } 
} 

<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'); 
    } 
} 

コントローラカスタムバリデーター 'checkOldPassword'は、ユーザーの現在のパスワードをチェックします。今問題はカスタム検証が動作していないということです。また、私はJavaScriptをオフにした場合私はサーバー側の検証エラーも表示されていないことがわかりました。 IiiはYiiに新しい。だから愚かな間違いを許してください。 助けてください。

+1

「されていませんどのように正確に "働く"? – 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)); 
     } 
    } 
} 

ます$ this->属性を交換

// 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'); 
    } 
} 

を... しかし、最初にそれを働かせてください。

+0

私はあなたのアプローチが良いbczを書いていないあなたはそれをチェックしているユーザーが入力されたパスワードを持っている場合は、同じものをリセットしている場合は...まずログインしたユーザーのパスワードを取得するユーザーからの入力と同じ場合はリセットしてください。$ this-> $属性または$ this-> attributes ['old_pwd']または$ this-> old_pwd –

+0

ありがとうございましたポインタ。しかし、これは単純なバックエンドシステムのためのものであり、管理者ユーザーは1人だけです。そのため、パスワードがdbに存在し、usernameには存在しないかどうかを確認しました。 – ajaybc

0

この試してみてください。それでもあなたapprochが良くありませんがます$ this-> PWD

+0

ありがとうございましたが、うまくいきませんでした。 – ajaybc

+0

私も以下を試しました。それでも動作しませんでした。私は他の何かが真剣に間違っていると思う。 'public function checkOldPassword($ attribute、$ params){ $ this-> addError($ attribute、'無効なパスワード '); } ' – ajaybc

0

まず私は、clientValidation古いパスワードをチェックしているため、サーバー側の検証(Ajax検証)が必要なため、動作しません。 clientValidationはので、このラインを置く

パスワードの長さをチェックするために良いとなどである: 「enableAjaxValidation」=> trueの場合、

第二に、私はこのような何かにあなたのコードを変更します

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"); 
} 
} 
} 
関連する問題