Home > Enterprise >  Method Illuminate\Validation\Validator::validateEnum does not exist
Method Illuminate\Validation\Validator::validateEnum does not exist

Time:09-19

I'm having a problem with the genre column during my form validation tests: Method Illuminate\Validation\Validator::validateEnum does not exist.

enter image description here

  • FighterRequest.php

    <?php
    
    namespace App\Http\Requests;
    
    use Illuminate\Foundation\Http\FormRequest;
    
    class FighterRequest extends FormRequest
    {
        /**
         * Determine if the user is authorized to make this request.
         *
         * @return bool
         */
        public function authorize()
        {
            return true;
        }
    
        /**
         * Get the validation rules that apply to the request.
         *
         * @return array<string, mixed>
         */
        public function rules()
        {
            return [
                'name' => 'required|max:30',
                'martial_art' => 'required|max:50',
                'nationality' => 'required|max:30',
                'genre' => 'required|enum',
                'height' => 'required|numeric',
                'weight' => 'required|numeric',
            ];
        }
        public function messages()
        {
            return[
                'name.required' => 'It's mandatory to define the name of the Fighter..',
                'name.max' => 'Fighter name must contain a maximum of 30 characters.',
                'martial_art.required' => 'It's mandatory to define the martial art of the Fighter.',
                'martial_art.max' => 'Fighter martial art must contain a maximum of 50 characters.',
                'nacionality.required' => 'It's mandatory to define the nationality of the Fighter.',
                'nacionality.max' => 'The Fighter's nationality must contain a maximum of 30 characters.',
                'genre.required' => 'It's mandatory to set the Fighter genre.',
                'genre.enum' => 'Fighter gender must be Male or Female.',
                'height.required' => 'It's mandatory to set the height of the Fighter.',
                'height.numeric' => 'Fighter height must be a numeric value.',
                'weight.required' => 'It is mandatory to set the Fighter weight.',
                'weight.numeric' => 'Fighter weight must be a numeric value.',
            ];
        }
    }

CodePudding user response:

Enum is a class based rule, and requires you to specify the Enum's type.

For example, let's say your enum is of type Gender:

        public function rules()
        {
            return [
                'name' => 'required|max:30',
                'martial_art' => 'required|max:50',
                'nationality' => 'required|max:30',
                'genre' => [
                    'required',
                    new Enum(Gender::class), // update with your actual class name
                ],
                'height' => 'required|numeric',
                'weight' => 'required|numeric',
            ];
        }

CodePudding user response:

From the Laravel documentation on the enum rule:

The Enum rule is a class based rule that validates whether the field under validation contains a valid enum value. The Enum rule accepts the name of the enum as its only constructor argument

Meaning you can't use it in the way you are. How is it to know the type of enum you're referring to etc.? Instead, use it as a class and pass your enum class to it as a parameter of its constructor:

'genre' => ['required', new Enum(Genre::class)],

Update

Ensure you've defined your enum correctly:

enum Genre: string
{
    case Male = 'Male';
    case Female = 'Female';
}

Then you should be fine to use it in your validation (don't forget to include a use statement in your FormRequest class).

A working example for you.

  • Related