Passing data around inside of validation closures

Posted 4 months ago by xtremer360

I'm trying to fun the following rules for a Form Request class and near the bottom in the closure, I need to make sure that the closure is ONLY running when the validation for the match_number passes. I'm also need to figure out after it passes how I can get access to the match_number value afte rit passes so that I can use it for the bottom closure inside of the winner_id rules.

public function rules()
    {
        // dd($this->all());
        return [
            'matches'                  => ['array'],
            'matches.*.match_number'   => [
                'required',
                'integer',
                'min:1',
                'distinct',
                function($attribute, $value, $fail) {
                    if (! Match::query()->where('match_number', $value)->where('event_id', request()->event->id)->exists()) {
                        return $fail("Match #{$value} does not exist for the event.");
                    }
                },
            ],
            'matches.*.match_decision_id' => ['required', 'integer', 'min:1', Rule::exists('match_decisions', 'id')],
            'matches.*.winner_id' => [
                'required',
                'integer',
                'min:1',
                Rule::exists('wrestlers', 'id'),
                function($attribute, $value, $fail) {
                    // dd('here');
                    dd($value);
                    if ($match = Match::query()->where('match_number', '?')->where('event_id', request()->event->id)->first()) {
                        if (! $match->wrestlers->contains('id', $value)) {
                            dd('not in match');
                            return $fail("The winner of match number {$match_number} was not involed in the match.");
                        }
                        // dd('in match');
                    }
                },
            ],
            'matches.*.result' => ['required', 'string'],
        ];
    }

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

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.