0

In my application I have a request file as below:

storeFactoryUser.php

public function rules()
    {
        return [
            'factory_users' => 'array',
            'factory_users.*.first_name' => 'required',
            'factory_users.*.last_name' => 'required',
            'factory_users.*.username' => 'required|alpha_dash|unique:users,username',
            'factory_users.*.f_user_email' => 'required|unique:users,email',
            'factory_users.*.external_ref' => 'required'
        ];
    }

Now when I enter a username that already exists, it returns an error message as below:

The factory_users.1.username has already been taken.

Instead of displaying it as above, I need to display it with the array value, for example:

Given factory user's username factory1 has already been taken.

To achieve this, I wrote a message function as below:

public function messages()
{
    $messages = [];
    foreach ($this->factory_users as $key => $factory_user) {
        $messages['factory_users.*.username.alpha_dash|unique:users,username'] = "Given factory user's username ".$factory_user['username'].' has already been taken.';
    }

    return $messages;
}

But still, it returns the same error message which I've mentioned above.

10
  • Have you tried this? stackoverflow.com/questions/37925592/… Commented Jan 18, 2021 at 6:36
  • yes, but it didn't help me. I would be much pleased if you could provide me an example :) Commented Jan 18, 2021 at 6:52
  • public function messages() { return [ 'factory_users.*.f_user_email.unique' => 'Given factory user's username XYZ has already been taken.', ]; } //Try this one and let me know Commented Jan 18, 2021 at 7:08
  • The factory_users.0.username has already been taken this is the response, it's not showing the message inside messages function :/ Commented Jan 18, 2021 at 8:32
  • Are you sure that your array key is 'f_user_email' ? show me your message function Commented Jan 18, 2021 at 8:46

1 Answer 1

0

You should use like this..

$factory_user['username']

instead of

$factory_user->username
Sign up to request clarification or add additional context in comments.

2 Comments

this solved one of my issues, now Trying to get property 'username' of non-object error is not thrown, but still, it is not displaying the error message inside messages function :/
I'm glad it helped you fix one of your problem. Will try to find why its not displaying the message. I guess its the message[] not cooked in proper way. notsure though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.