Home > OS >  Laravel 9 php 8.1 enum validation
Laravel 9 php 8.1 enum validation

Time:09-23

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 >
      <label for="role">Role</label>
         <select 
             
            id="role"
            v-model="game.role"
            required
      >
       <option  type="radio" value='employee'>Employee</option>
       <option  type="radio" value='manager'>Manager</option>
       <option  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.

CodePudding user response:

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!',
        ]);
    }
}

  • Related