【问题标题】:Laravel 4 Validation unique(database) ignore currentLaravel 4 验证唯一(数据库)忽略当前
【发布时间】:2013-06-03 07:04:24
【问题描述】:

我正在对用户编辑表单进行验证,并让验证检查用户名在数据库中是否唯一。

但是,当编辑用户并且您不更改用户名字段时,它仍在检查该字段是否唯一...

如果不更改,是否有一种 laravel 方法可以忽略当前用户名?

【问题讨论】:

    标签: validation laravel laravel-4


    【解决方案1】:

    应该更多地阅读文档,当然 laravel 4 会这样做......

    我有什么

    $rules = array('username' => 'required|unique:users');
    

    我有什么

    $rules = array('username' => 'required|unique:users,username,'.$id);
    

    您可以告诉验证器忽略上面的给定 ID。

    【讨论】:

    • 你也可以这样做 $rules = array('username' => 'required|unique:users,username,{{$id}}');
    • 这帮了很多忙。谢谢!
    • 应该有更好的方法来做到这一点(我想我以前做过,只是不记得了)。我将每个模型规则都保存在 $rules 属性上,并且我想在更新时检查唯一性时自动跳过自身。
    【解决方案2】:

    您好,您可以将验证器用作服务或复制逻辑并应用您的需要,就像这样

    $valitator = MyCustomValidator::make()->on('update')->setCurrent($id); 
    

    签到: https://github.com/bradleySuira/laravel-context-validator/blob/master/examples/unique-on-update.php

    【讨论】:

    • 调用未定义的方法::make()
    • 可能是示例中的命名空间是 App\Services\Validators\EquipmentTypeValidator;您应该添加 use 关键字
    【解决方案3】:

    这个解决方案对我有用:

    protected  $rules = array(
        'email' => "required|email|unique:users,email,:id",
    );
    

    【讨论】:

    • 这在 Laravel 5.0 上运行良好。使用 ":id" 表示法意味着您不需要知道记录的 id 是什么。当验证规则是静态定义时特别方便 - 在配置文件或 eloquent 模型的静态 $rules 数组中,并且代码中没有任何地方可以传递更新记录 ID,即使您确实知道它验证点。
    • 这对我来说是开箱即用的,我认为你应该做类似this的事情。
    【解决方案4】:

    对于 Laravel 4.1,如果您扩展 Illuminate\Validation\Validator 类,请像这样覆盖 validateUnique() 方法:

    /**
     * Validate the uniqueness of an attribute value on a given database table.
     *
     * If a database column is not specified, the attribute will be used.
     *
     * @param  string  $attribute
     * @param  mixed   $value
     * @param  array   $parameters
     * @return bool
     */
    protected function validateUnique($attribute, $value, $parameters)
    {
        $this->requireParameterCount(1, $parameters, 'unique');
    
        $table = $parameters[0];
    
        // The second parameter position holds the name of the column that needs to
        // be verified as unique. If this parameter isn't specified we will just
        // assume that this column to be verified shares the attribute's name.
        $column = isset($parameters[1]) ? $parameters[1] : $attribute;
    
        // If the specified ID column is present in the data, use those values; otherwise,
        // fall back on supplied parameters.
        list($idColumn, $id) = [null, null];
        if (isset($parameters[2])) {
            list($idColumn, $id) = $this->getUniqueIds($parameters);
    
            if (strtolower($id) == 'null') $id = null;
            else if (strtolower($id) == 'id' && isset($this->data[$idColumn])) $id = $this->data[$idColumn];
        }
        else if (isset($this->data['id'])) {
            $idColumn = 'id';
            $id = $this->data[$idColumn];
        }
    
        // The presence verifier is responsible for counting rows within this store
        // mechanism which might be a relational database or any other permanent
        // data store like Redis, etc. We will use it to determine uniqueness.
        $verifier = $this->getPresenceVerifier();
    
        $extra = $this->getUniqueExtra($parameters);
    
        return $verifier->getCount(
    
            $table, $column, $value, $id, $idColumn, $extra
    
        ) == 0;
    }
    

    在您的验证器规则中,您可以这样做(将检查验证数据中是否存在 id 字段 - 这是最通用的用法):

    $rules = 'unique:model,field';
    

    this(将检查验证数据中是否存在id 字段)(相当于上面的):

    // specifying anything other than 'id' or 'null' as the 3rd parameter
    // will still use the current behavior
    $rules = 'unique:model,field,id'; 
    

    或者这个(将检查验证数据中是否存在idColumn 字段):

    // specifying anything other than 'id' or 'null' as the 3rd parameter
    // will still use the current behavior
    $rules = 'unique:model,field,id,idColumn';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-14
      • 2021-02-07
      • 1970-01-01
      • 2020-09-13
      • 1970-01-01
      • 2018-10-26
      • 2021-11-05
      • 1970-01-01
      相关资源
      最近更新 更多