pn
1 month ago
134
5
Laravel

Error in array valdiation

Posted 1 month ago by pn

I am building api and trying to update user roles without detaching

Input:

{
    "roles": [
        "1",
        "2"
    ]
}

Error:

{
    "message": "The given data was invalid.",
    "errors": {
        "name": [
            "The name field is required when none of username / email / password / email verified at / roles are present."
        ],
        "username": [
            "The username field is required when none of name / email / password / email verified at / roles are present."
        ],
        "email": [
            "The email field is required when none of username / name / password / email verified at / roles are present."
        ],
        "password": [
            "The password field is required when none of name / username / email / email verified at / roles are present."
        ],
        "email_verified_at": [
            "The email verified at field is required when none of name / username / email / password / roles are present."
        ]
    }
}

My validation:

$validatedData = $request->validate([
            'name' => 'required_without_all:username, email, password, email_verified_at, roles|string',
            'username' => 'required_without_all:name, email, password, email_verified_at, roles|string|unique:users,username',
            'email' => 'required_without_all:username, name, password, email_verified_at, roles|string|unique:users,email',
            'password' => 'required_without_all:name, username, email, email_verified_at, roles|string',
            'email_verified_at' => 'required_without_all:name, username, email, password, roles|string|timezone',
            'roles.*' => 'required_without_all:name, username, email, password, email_verified_at|exists:roles,id',
        ]);

blah blah blah ...

if ($request->has('roles')) {
            $user->roles()->syncWithoutDetaching($request->roles);
        }

        $userUpdated = $user->update();

What is wrong in the code ?

Please sign in or create an account to participate in this conversation.