【问题标题】:Yii Model rule for validation doesn't workYii 模型验证规则不起作用
【发布时间】:2014-01-17 20:22:24
【问题描述】:

我是 Yii 的新手。我已经创建了密码更改表单,页面名称是更改。在该页面中,有旧密码、新密码和确认新密码三个字段。

我的规则

    public function rules() {
    return array(
        array('old_password, confirm_password, str_user_password', 'required',
            'on' => 'change'),
        array('confirm_password', 'compare', 'compareAttribute' => 'str_user_password',
            'message' => 'Password Must Be same.', 'on' => 'change'),
    );
}

控制器

public function actionChange($id)
    {
        $model=new User('change');
        //$model->setScenario('change');
        $model=$this->loadModel($id);
        if(isset($_POST['User']))
            {
                $model->attributes=$_POST['User'];
                if($model->save())
                $this->redirect(array('view','id'=>$model->int_user_id));

            }

        $this->render('change',array(
            'model'=>$model,
        ));
    }

创建和更新页面验证工作。我已将其复制到更新页面中,它运行良好。我认为是场景问题。请帮助我找到解决方案。

【问题讨论】:

  • 我认为你的 loadModel 位置的问题比设定的场景更重要。 loadmodel 创建新模型,您的模型场景被此覆盖
  • @naveengoyal nope.. 我不认为它会解决问题。
  • 你能把表格和整个模型类贴出来吗?
  • @Letmesee 看看加载模型代码。它使用 findbypk 方法。创建新模型
  • @naveengoyal 是的,你是对的。将此作为答案发布兄弟。这是解决方案

标签: php yii


【解决方案1】:

试试这个... 我认为 Load 模型会创建覆盖您的场景的新模型。

    $model=$this->loadModel($id);
    $model->setScenario('change');

【讨论】:

  • 谢谢问题解决
【解决方案2】:

您必须将新密码与确认密码字段进行比较:

array('confirm_password', 'compare', 'compareAttribute' => 'new_password',
        'message' => 'Password Must Be same.', 'on' => 'change'),

array('new_password', 'compare', 'compareAttribute' => 'confirm_password',
        'message' => 'Password Must Be same.', 'on' => 'change'),

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多