最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Laravel validation call `after()` function if `rules()` validates successfully - Stack Overflow

programmeradmin0浏览0评论

I'm using laravel 11. What I want is to validate the rules in the rules() function first before running the after() function.

I have the following code below:

class UpdateUserRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            "first_name" => "required",
            "last_name" => "required",
            "user_number" => "required|exists:users,user_number",
        ];
    }

    public function after(): array
    {
        return [
            function (Validator $validator) {
                $user = User::whereUserNumber($this->user_number)->first();

                dd($user);

                // Some checking logic below
            }
        ];
    }
}

I followed the documentation here: .x/validation#performing-additional-validation-on-form-requests

What I want here is to validate the user_number if it exists in the users table then perform some checking in the after function When I entered an empty user_number its still calling the code inside the after() function (the value of $user is null).

Based on the documentation it says here that the after method is called after validation is complete. But its not doing what I thought it to be.

Is there a elegant way to do this on laravel?

I'm using laravel 11. What I want is to validate the rules in the rules() function first before running the after() function.

I have the following code below:

class UpdateUserRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            "first_name" => "required",
            "last_name" => "required",
            "user_number" => "required|exists:users,user_number",
        ];
    }

    public function after(): array
    {
        return [
            function (Validator $validator) {
                $user = User::whereUserNumber($this->user_number)->first();

                dd($user);

                // Some checking logic below
            }
        ];
    }
}

I followed the documentation here: https://laravel/docs/11.x/validation#performing-additional-validation-on-form-requests

What I want here is to validate the user_number if it exists in the users table then perform some checking in the after function When I entered an empty user_number its still calling the code inside the after() function (the value of $user is null).

Based on the documentation it says here that the after method is called after validation is complete. But its not doing what I thought it to be.

Is there a elegant way to do this on laravel?

Share Improve this question asked Jan 29 at 23:38 aceraven777aceraven777 4,5064 gold badges38 silver badges58 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Based on the documentation it says here that the after method is called after validation is complete.

Yep, unfortunately, after is called not after but along with other parts of this validation.

I solved this case with checking validation errors in after() before additional checks.
This relies on MessageBag and does not trigger repeated validation.

    public function after(): array
    {
        return [
            function (Validator $validator) {

                // validation errors from previous steps will be there:
                if ($validator->errors()->isNotEmpty()) {
                    return;
                }

                $user = User::whereUserNumber($this->user_number)->first();

                dd($user);

                // Some checking logic below
            }
        ];
    }
发布评论

评论列表(0)

  1. 暂无评论