How to make validation rules for storing and update users if there are unique attributes

蓝咒 提交于 2019-12-08 13:34:53

问题


I use username as an id to login and made username and email as unique. Whenever I made username and email, validation rules check coming data and there is no problem with it because username and email would be new and unique. The problem occurs when I update. when I update the attributes, I don't need to change the username mostly but the rules are expecting unique, username. what is the best practice for updating unique attributes with validation rules?.

Update user method

 public function updateUser($req, $id)
    {
        $user = User::where('user_id', $id)->firstOrFail();
        $user->username = $req->input('username');
        $user->password = Hash::make($req->input('password'));
        $user->email = $req->input('email');
        $user->first_name = $req->input('first_name');
        $user->last_name = $req->input('last_name');
        $user->phone = $req->input('phone');
        $user->emergency_phone = $req->input('emergency_phone');
        $user->profile_photo = $req->input('profile_photo');
        $user->role = $req->input('role');
        $user->status = $req->input('status');

        $user->save();
        return $this->filterUsers([$user]);
    }

Rules

  protected $rules = [

        'username' => 'required|max:20|unique:users',
        'email' => 'required|string|email|max:255|unique:users',
        'password' => 'required',
        // **in Production **
        // 'password' => [
        //     'required',
        //     'min:6',
        //     'regex:/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[\d\X])(?=.*[!$#%]).*$/',
        //     'confirmed'
        // ],
        /**
         * Three of the five rules. 
         * English uppercase characters (A – Z)
         * English lowercase characters (a – z)
         * Base 10 digits (0 – 9)
         * Non-alphanumeric (For example: !, $, #, or %)
         * Unicode characters
         */

        'first_name' => 'required|string|max:255',
        'last_name' => 'required|string|max:255',
        'phone' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:5',
        'role' => 'required'


    ];

回答1:


You can use

'username' => 'required|max:20|unique:users,username,' . $id',

if your users table primary key is id, if it is something else like user_id, then it will become

'username'  =>  'required|email|unique:user,username,' . $request->input('user_id') . ',userid',

so syntax is

'input_field' => 'unique:<table name>,<column name for this input field>, <unique id>, <unique id column in table>';



回答2:


At edit time it will check for a unique username so we avoid unique for a current edit id

In the update method your validation rules as per below:

protected $rules = [
        'username' => 'required|max:20|unique:users,username,' . $id',
        'email' => 'required|string|email|max:255|unique:users,email,' . $id',
        'password' => 'required',
        'first_name' => 'required|string|max:255',
        'last_name' => 'required|string|max:255',
        'phone' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:5',
        'role' => 'required'
    ];



回答3:


You need to use laravel validator class in controller Use Validator;

And use something like this:

$error = Validator::make($req->all(), $rules);

More examples here in docs of Validation.



来源:https://stackoverflow.com/questions/56204272/how-to-make-validation-rules-for-storing-and-update-users-if-there-are-unique-at

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!