How to forcing a unique rule to ignore a given id?

laravel validation unique ignore id
laravel unique validation on update
laravel validation unique string
unique column validation in laravel
laravel form request validation unique update
what does validation unique mean
unique email id validation in laravel
laravel validation rule::unique where

While updating the "users" table, I want the EditUserRequest form checkout to ignore the current email because it already exists. Code rules:

public function rules()
{
    return [
        'username' => 'required|max:200',
        'email' =>  [
                        'required',
                        Rule::unique('users')->ignore($user->id),
                    ],
        'pass1' => 'required|min:6',
        'pass2' => 'same:pass1',
    ];
}

Code UserController:

public function postEdit($id,EditUserRequest $request)
{
    $user = User::find($id);
    $user->name = $request->username;
    $user->email = $request->email;
    $user->password = Hash::make($request->pass1);
    $user->level = $request->get('cbadmin',0);
    $user->save();
    return view('admin.edit-user')->with('success','Sửa thành công!');
}

This line: 'email' => 'required|email|unique:users,email' will check email exists. However, the default email I need to update it already exists. Did I read:https://laravel.com/docs/5.6/validation#rule-unique. But I do not understand how to solve this problem

public function rules()
    {
        return [
            'username' => 'required|max:200',
            'email' => 'required|email|unique:users,email,'.request()->user()->id,
            'pass1' => 'required|min:6',
            'pass2' => 'same:pass1',
        ];
    }

You can pass a third parameter as id to ignore.

Hope this helps.

Forcing a unique rule to ignore a given id, Forcing A Unique Rule To Ignore A Given ID #15518. Closed. iamayaz opened this issue on Sep 20, 2016 · 10 comments. Closed  In my controller i was trying to update my product, product name here is unique and here i wanted to update only price. so name will be as it but as i defined name as a unique field though i haven't

you can do this by using the Rule facade ..

'email' => [
    'required',
    Rule::unique('users')->ignore($user->id),
],

EDIT

in case you're using a function to validate your inputs you may do it like this:

protected function yourValidatorName(array $data, $id)
{
    return Validator::make($data, [
        'username' => 'required|max:200',
        'email' =>  [
            'required',
             Rule::unique('users')->ignore($id),
        ],
        'pass1' => 'required|min:6',
        'pass2' => 'same:pass1',
    ]);
}

then

public function postEdit($id, EditUserRequest $request)
{
    $accountValidate = $this->yourValidatorName($request->all(), $id);
    if (!$accountValidate->fails())
    {
        // success
    }
    else
    {
        // failed
    }
}

Forcing A Unique Rule To Ignore A Given ID · Issue #15518 · laravel , Hi,. According to the Laravel documentation, I can do: 'email' => 'unique:users,​email_address,10'. and the unique rule will only be tested if the id value is not 10. "Sometimes, you may wish to ignore a given ID during the unique check. For example, consider an "update profile" screen that includes the user's name, e-mail address, and location. Of course, you will want to verify that the e-mail address is unique.

Can you show me your route for postEdit?

For example this is your route:

Route::put('users/{user_id_sample}', 'UserController@postEdit');

You may do something like this:

public function rules()
{
    return [
        'username' => 'required|max:200',
        'email' =>  [
                        'required',
                        Rule::unique('users')->ignore($this->route('user_id_sample')),
                    ],
        'pass1' => 'required|min:6',
        'pass2' => 'same:pass1',
    ];
}

Forcing A Unique Rule To Ignore A Given ID, Domains Terminology About · Laravel Validation: Available Validation Rules. Available Validation Rules: Forcing A Unique Rule To Ignore A Given ID:  Laravel Version: 5.2. PHP Version: 5.4.6 Description: I have table in which i'm trying to store email addresses. These email addresses will be save with user_id. For example in email_list table

I am not sure of your route parameter names since your routes aren't included in the post, so I will assume $id on the controller method means there is a route parameter named id.

'email' =>  [
    'required',
    Rule::unique('users')->ignore($this->route('id')),
],

This would use the id from the route parameter, assuming it was named id. This will tell the rule to ignore the unique check on this email field for this particular User id.

Available Validation Rules: Forcing A Unique Rule To Ignore A , Both unique and exists validation rules, require the database to be queried. This packages Forcing A Unique Rule To Ignore A Given ID: Sometimes, you may  Forcing A Unique Rule To Ignore A Given ID Inside Of A Request Form Posted 3 years ago by KHAN I have an event resource where the invite code should be unique to each event.

[orm] Validation, There is another value we can add to the unique validation rule which is except to say which model to ignore. We could write this a couple of  To reproduce, define some validation rules that use the "unique" rule: The rule fails to generate the proper query if the referenced table does not rely on a primary key named "id". E.g. if your table is defined using a primary key of "user_id", the query and validation rule fails. Even if the model includes.

Using the unique validation rule in a Laravel Form Request, You may also access an array of the failed validation rules, without messages. To do so, use the failed method: Forcing A Unique Rule To Ignore A Given ID. Forcing A Unique Rule To Ignore A Given ID: Sometimes, you may wish to ignore a given ID during the unique check. For example, consider an "update profile" screen that includes the user's name, e-mail address, and location. You will probably want to verify that the e-mail address is unique.

Validation - Laravel, Forcing A Unique Rule To Ignore A Given ID: Sometimes, you may wish to ignore a given ID during the unique check. For example, consider an “update profile”  The field under validation must be unique on a given database table. If the column option is not specified, the field name will be used. Basic usage of unique rule 'email' => 'unique:users' Specifying a custom column name 'email' => 'unique:users,email_address' Forcing a unique rule to ignore a given ID 'email' => 'unique:users,email_address,10'

Comments
  • Possible duplicate of Field UNIQUE (already taken) in edit?
  • What kind of error are you getting?
  • please post how you do your validation .. there are different types ..
  • I have modified the code above. But it still fails. You review your help with
  • I have modified the code above. But it still fails. You review your help with
  • form requests get validated when resolved from the container. the validator is created and validated before you even get to the first line of that method, postEdit.
  • request got to the postEdit gets validated before saving if success ..