1

I have been learning the new enum class and been having a heck of a time getting it to pass validation. Its just a simple role enum. The error I'm getting says "The selected role is invalid even though I'm seeing in console.log(employee) that it is a valid value for each selection.

The Enum

<?php
namespace App\Enums;

enum RoleEnum: string
{
    case none      = 'none'; //in case role has yet to be assigned
    case employee  = 'employee';
    case manager   = 'manager';
    case admin     = 'admin';
}

The model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Boss;
use App\Enums\RoleEnum;

class Employee extends Model
{
    use HasFactory;
    protected $fillable = [ 'id', 'name', 'boss_id','title' ];
    protected $casts = [ 'role' => RoleEnum::class];
    
    public function employees()
    {
        return $this->belongsTo('\App\Models\Boss');
    }
}

The Controller

    <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Models\Boss;
use App\Models\Employee;
use App\Enums\RoleEnum;

class EmployeeController extends Controller
{
public function store(Request $request)
    {
         $request->validate([
            'name'       =>'required|string|max:255',
            'boss_id'    =>'required|exists:bosses,id',
            'title'      =>'string|max:255',
            'role'       =>'required|in:RoleEnum',
        ]); 
        $employee = Employee::create([
            'name'       => $request->name,
            'boss_id'    => $request->boss_id,
            'title'      => $request->title,
            'role'       => $request->role,
        ]);
        $bosses = Boss::get();
        return redirect('/details')->with([
            'employee' => $employee,
            'bosses'   => $bosses,
            'success','User Created!',
        ]);
    }
}

The Create blade input (I only included the code in question)

   <div class="form-group">
      <label for="role">Role</label>
         <select 
            class="form-control" 
            id="role"
            v-model="game.role"
            required
      >
       <option class="form-check-input" type="radio" value='employee'>Employee</option>
       <option class="form-check-input" type="radio" value='manager'>Manager</option>
       <option class="form-check-input" type="radio" value='admin'>Admin</option>
      </select>
  </div>

Consol.log(employee)

name: "John Martin"
boss_id: "5"
title: "Trainer"
role: "employee"

This is all new territory for me so any help is greatly appreciated.

CasaCoding
  • 119
  • 1
  • 2
  • 15
  • Does this answer your question? [Method Illuminate\Validation\Validator::validateEnum does not exist](https://stackoverflow.com/questions/73773890/method-illuminate-validation-validatorvalidateenum-does-not-exist) – Peppermintology Sep 21 '22 at 09:09

1 Answers1

2

The in rule you are using is for a list of specific comma-separated values. You are passing it the name of an enum however in does not work like that.

Laravel has an enum validation rule you can use:

use Illuminate\Validation\Rules\Enum;
use App\Enums;

class EmployeeController extends Controller
{
    public function store(Request $request)
    {
         $request->validate([
            'name'       =>'required|string|max:255',
            'boss_id'    =>'required|exists:bosses,id',
            'title'      =>'string|max:255',
            'role'       => [ 'required', new Enum(RoleEnum::class) ],
        ]); 
        $employee = Employee::create([
            'name'       => $request->name,
            'boss_id'    => $request->boss_id,
            'title'      => $request->title,
            'role'       => $request->role,
        ]);
        $bosses = Boss::get();
        return redirect('/details')->with([
            'employee' => $employee,
            'bosses'   => $bosses,
            'success','User Created!',
        ]);
    }
}

apokryfos
  • 38,771
  • 9
  • 70
  • 114
  • Worth noting that the `select` element in the question will require the `name="role"` attribute added as it is currently missing resulting in the `required` validation rule failing. Just in case someone tries this and thinks your implementation of the `new Enum()` rule is to blame, which it isn't. – Peppermintology Sep 21 '22 at 09:18
  • Yes I tried this setup originally following the docs but it gives me an error "Class \"App\\Http\\Controllers\\Enum\" not found" – CasaCoding Sep 21 '22 at 09:48
  • 1
    Note the `use Illuminate\Validation\Rules\Enum;` before the class. If you still get that error make sure you run `composer update` to get the latest Laravel 9 minor version – apokryfos Sep 21 '22 at 10:03
  • I still can't save the enum value with this code. The error is gone but know it says that the role is empty. I've dd'd my $request->role and it comes back as the correct value.? Any Ideas? – CasaCoding Sep 23 '22 at 08:36
  • 1
    To convert the string value to that enum you'd need to do `RoleEnum::from($request->role)` – apokryfos Sep 23 '22 at 09:04