首页 文章

Laravel 4 Validation unique(数据库)忽略当前

提问于
浏览
20

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

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

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

4 回答

  • 28

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

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

    登记入住:https://github.com/bradleySuira/laravel-context-validator/blob/master/examples/unique-on-update.php

  • 0

    这个解决方案对我有用:

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

    对于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';
    

    这(将检查验证数据中存在的 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';
    
  • 1

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

    我拥有什么

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

    我有什么

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

    您可以告诉验证者忽略上面给出的ID .

相关问题