【问题标题】:Exist Validation doesn't work in Yii存在验证在 Yii 中不起作用
【发布时间】:2017-01-08 17:56:30
【问题描述】:

我在 Yii 中存在验证问题。当我运行我的代码时,没有触发存在验证。不知道是什么问题。

这是用户模型中的规则:

public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('id_cabang, username, password, nama, level', 'required', 'on'=>'create'),
            array('username, nama, level', 'required', 'on'=>'update'),
            array('id_cabang, nohp', 'numerical', 'integerOnly'=>true),
            array('username', 'length', 'max'=>20),
            array('password', 'length', 'max'=>20),
            array('nama', 'length', 'max'=>100),
            array('jabatan', 'length', 'max'=>100),
            array('nohp', 'length', 'max'=>14),
            array('level', 'length', 'max'=>10),
            array('username', 'exist', 
                'attributeName'=>'username', 
                'className'=>'User', 
                'caseSensitive'=>true, 
                'on'=>'create'
            ),
            array('username', 'exist', 
                'attributeName'=>'username', 
                'className'=>'User', 
                'caseSensitive'=>true, 
                'criteria'=>array('condition'=>'username<>:u', 'params'=>array(':u'=>$this->current_username)),
                'on'=>'update'
            ),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id_cabang, username, nama, nohp, jabatan, level', 'safe', 'on'=>'search'),
        );
    }

这是我在用户控制器中的 actionCreate:

public function actionCreate()
    {
        $model=new User;
        $model->scenario = 'create';
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['User']))
        {
            $model->attributes=$_POST['User'];
            $model->id_cabang = 1;
            if($model->save())
                $this->redirect(array('index'));
        }

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

请帮帮我..

谢谢。

【问题讨论】:

    标签: php validation yii


    【解决方案1】:

    如果你想要一些列是必需的,你应该写你的代码

    public function rules()
        {
            // NOTE: you should only define rules for those attributes that
            // will receive user inputs.
            return array(
                array(array('id_cabang', 'username', 'password', 'nama','level'), 'required','on'=>'create'),
            );
        }
    

    【讨论】:

      【解决方案2】:

      要回答这个问题,我们需要知道您想要完成什么。根据您现有的更新验证器参数,我猜您尝试检查或唯一性。

      如果您需要检查用户名在所有记录中是否唯一,那么有一个验证器:http://www.yiiframework.com/doc/api/CUniqueValidator。

      否则你需要指定你想要什么,并检查这个参考: http://www.yiiframework.com/wiki/56/reference-model-rules-validation/

      【讨论】:

      • 是的,你说得对。我想在添加新用户时检查用户名到数据库。我将尝试使用唯一的验证器。谢谢。
      • @RizaldiMaulidia 如果这个答案或任何其他答案对您有帮助或解决了您的问题,您可以接受。
      • 成功了!谢谢。我尝试使用唯一验证。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多