0

I want to validate a value that I got from a certain form. The value type is text. I want it to match a specific username from the database from the users table, but also to not match the current user's username.

To achieve that, I used the following validation rules:

'username' => [
    'required',
    'string',
    'exists:App\User,username',
    'different:' . auth()->user()->username
]

What I've discovered is that whenever the auth()->user()->username value includes a digit, it passes the validation even if request()->username = auth()->user()->username. Is there anything I can do to prevent this from happening?

Thanks in advance.

RCRalph
  • 355
  • 6
  • 17

3 Answers3

4

Use unique like -

Considering id is your user's id.

'username' => 'required|string|unique:username,id,'.auth()->user()->username,

This will check if username is unique or not except this userId.

Saurabh Gupte
  • 238
  • 1
  • 3
  • 17
Abhishek Honrao
  • 780
  • 5
  • 28
  • I'm afraid this won't work for me. I want it to refer to an existing record in the database, which `unique` doesn't support. – RCRalph Jun 17 '20 at 23:16
  • 1
    ```unique``` will consider in exsting records as well. refer - https://laravel.com/docs/7.x/validation – Abhishek Honrao Jun 17 '20 at 23:18
  • Even if that was true, this validation doesn't allow existing usernames to be entered. – RCRalph Jun 17 '20 at 23:19
  • Though I changed my formula to something that works for my case: `'username' => ['required', 'string', 'unique:users,username,'.auth()->user()->username]` – RCRalph Jun 17 '20 at 23:20
  • 1
    Chekout this - https://stackoverflow.com/questions/52335632/validate-unique-email-on-update-ignoring-self-email-on-laravel-5 – Abhishek Honrao Jun 17 '20 at 23:23
  • Just to make it clear, I want it to work like this does: `'username' => ['required', 'string', 'exists:App\User,username']` but to error out if `request()->username = auth()->user()->username`. – RCRalph Jun 17 '20 at 23:30
  • 1
    What do you mean? I am not getting you. Can your elaborate, please? – Abhishek Honrao Jun 17 '20 at 23:31
  • I would like to create a transaction between two users. The current user will be sending the money and the specified one will be receiving them. I don't want the user to send money to himself (obviously), but I also don't want to send money to someone who doesn't exist in the database. – RCRalph Jun 17 '20 at 23:34
  • The `username` field provides the recipient's username. – RCRalph Jun 17 '20 at 23:35
  • 1
    This may help you - https://stackoverflow.com/questions/26121417/laravel-validation-exists-with-additional-column-condition-custom-validation – Abhishek Honrao Jun 17 '20 at 23:38
  • I was actually looking for something else, I already posted an answer to my own question so you can see what I really meant. Thanks for help anyways. – RCRalph Jun 17 '20 at 23:50
2

The answer to this issue was creating own Rule::exists validation, which is shown below:

'username' => [
    'required',
    'string',
    Rule::exists('users')->where(function ($query) {
        $query->where('username', '<>', auth()->user()->username);
    })
],
RCRalph
  • 355
  • 6
  • 17
1

I solved a similar problem as follows.

$request->validate([
            'email' => ['required', 'email','unique:users,email,'.Auth::id()],
            'phone' => ['required', 'unique:users,phone,'.Auth::id()],
        ]);
pinokyov
  • 11
  • 2