Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ignore some fileds when updating data

how can i ignore username / email / phone validation check for current user when updating?

update: i also used class validation but user is null and cant reach the current user id there

updateUserMutator(input: UpdateUserInput! ): User  @guard(with: ["api"])


input UpdateUserInput {
    name: String! @rules(apply: ["min:1","max:255", "required"])
    lastname: String! @rules(apply: ["min:1", "max:255", "required"])
    username: String! @rules(apply: ["min:1","max:12", "unique:users,username", "regex:/^[a-zA-Z0-9]+/"])
    email: String! @rules(apply: ["email", "unique:users,email", "required"])
    phone: String! @rules(apply: ["required", "unique:users,phone", "regex:/[0-9]{10}/"])
}


used class validation:

final class UpdateUserInputValidator extends Validator
{
    public function authorize()
    {
        return true;
    }

    /**
     * Return the validation rules.
     *
     * @return array<string, array<mixed>>
     */
    public function rules(): array
    {

        // get user from context

        $id = auth()->id();
        return [
            'name' => ['sometimes', 'string', 'between:2,25'],
            'lastname' => ['sometimes', 'string', 'between:2,25'],
            'username' => [
                'sometimes',
                Rule::unique('users', 'username')->ignore($id, 'id'),
            ],
            'email' => [
                'sometimes',
                'string',
                'email',
                'max:255',
                Rule::unique('users', 'email')->ignore($id, 'id'),
            ],
            'phone' => [
                'sometimes',
                'regex:/[0-9]{10}/',
                Rule::unique('users', 'phone')->ignore($id, 'id'),
            ],

        ];
    }
}

but user is null there.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You forget to update the upper part

input UpdateUserInput @validator {
    name: String
    lastname: String
    username: String
    email: String
    phone: String
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading