【问题标题】:Yii custom validation and server side validation?Yii 自定义验证和服务器端验证?
【发布时间】: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


【解决方案1】:
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));
        }
    }
}

虽然你的方法仍然不好......但首先让它工作......

【讨论】:

  • 我写了你的方法不好 bcz 你正在检查它是否有任何用户输入了密码,如果你正在重置相同的密码......而不是你应该做的是首先获取密码登录用户,如果它与用户的输入相同..然后重置它..您可以使用 $this->$attribute 或 $this->attributes['old_pwd'] 或 $this->old_pwd
  • 感谢您的指点。但问题是这是一个简单的后端系统,只有一个管理员用户。这就是为什么我检查数据库中是否存在密码而不是用户名。
【解决方案2】:

试试这个:

// 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->attribute 替换为 $this->pwd

【讨论】:

  • 我也尝试了以下方法。即使这样也没有用。我认为还有其他严重错误。 '公共函数 checkOldPassword($attribute,$params){ $this->addError($attribute, '无效密码'); }'
【解决方案3】:

首先,我认为 clientValidation 将不起作用,因为您正在检查旧密码,因此您需要服务器端验证(Ajax 验证)。 clientValidation 有利于检查密码长度等。

所以把这一行: 'enableAjaxValidation'=>真,

其次,我会将您的代码更改为以下内容:

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

【讨论】:

    猜你喜欢
    • 2017-05-03
    • 1970-01-01
    • 2018-07-23
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多