Custom Rule Class

Posted 5 months ago by xtremer360

I have a feeling that I have written the follow Rule class incorrectly and could use some guidance and what I've done wrong because I am not achieving my desired results.

What I'm trying to achieve is validate a form with my custom request class called MatchCreateFormRequest which I have included below. The section that is not correct is when I am trying to validate that a wrestler is qualified for a match at an event that the match belongs to.

Each event contains a date. Each wrestler has a hired_at date.

The requirement is the wrestler must have been hired before the event is scheduled to take place.

From my test all it says is there's no error missing from the session but there should be with that Rule.

Any suggestions on where I"m going wrong with this?

<?php

namespace App\Rules;

use Carbon\Carbon;
use Illuminate\Contracts\Validation\Rule;

class QualifiedForMatch implements Rule
{
    /**
     * @var \App\Models\Event
     */
    private $eventDate;

    /**
     * @var \App\Models\*
     */
    private $model;

    /**
     * Create a new rule instance.
     *
     * @param $eventDate
     */
    public function __construct($eventDate, $model)
    {
        $this->eventDate = $eventDate;
        $this->model = $model;
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $model)
    {
        return $model->$attribute->lte(Carbon::parse($this->eventDate));
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'This '.strtolower(class_basename(get_class($this->model))).' is not qualified for the match.';
    }
}

<?php

namespace App\Http\Requests;

use App\Models\MatchType;
use App\Models\Wrestler;
use App\Models\Event;
use App\Rules\QualifiedForMatch;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Validator;

class MatchCreateFormRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return $this->user()->hasPermission('store-match');
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        // dd($this->all());
        $date = Event::where('id', $this->event_id)->first()->date;

        $rules = [
            'matches' => 'array',
            'matches.*.match_number' => 'required|integer|min:1',
            'matches.*.match_type_id' => 'required|integer|exists:match_types,id',
            'matches.*.stipulations' => 'array',
            'matches.*.stipulations.*' => 'sometimes|distinct|integer|exists:stipulations,id',
            'matches.*.titles' => 'array',
            'matches.*.titles.*' => 'sometimes|distinct|integer|exists:titles,id',
            'matches.*.referees' => 'required|array',
            'matches.*.referees.*' => 'distinct|integer|exists:referees,id',
            'matches.*.preview' => 'required|string',
        ];

        if (is_array($this->matches)) {
            $i = 0;
            foreach ($this->matches as $match) {
                if (!is_null($match['match_type_id']) && is_integer($match['match_type_id']) && MatchType::where('id', $match['match_type_id'])->exists()) {
                    $total = (int) MatchType::where('id', $match['match_type_id'])->first()->total_competitors;

                    $rules["matches.{$i}.wrestlers"] = 'required|array|size:' . $total;
                    $i++;
                }

                dd($match['wrestlers']);
                foreach($match['wrestlers'] as $key => $wrestlerId) {
                    $exists = Wrestler::where('id', $wrestlerId)->exists();
                    if ($exists) {
                        $wrestler = Wrestler::where('id', $wrestlerId)->first();
                        $rules["matches.{$i}.wrestlers"] = [
                            'distinct',
                            'integer',
                            'exists:wrestlers,id',
                            new QualifiedForMatch($date, $wrestler)
                        ];
                    }
                }
            }
        }

        return $rules;
    }
}

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.