Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cris_lml's avatar

error of Validations

I'm having problems with my validations, I get the error: Cannot instantiate enum App\Enums\Type ; The funny thing is that I don't instantiate the enum at any time, after debugging I came to the conclusion that the error is in my mutator: protected function type(): Attribute { return Attribute::make( get: fn(int $value) => $value==Type::CONTRAIN ? 'Contraindication' : 'Side Effect' ); } But something I can't understand is why it is validating or trying to validate something that is not marked for validation: #[Validate('required')] public $name;

#[Validate('required')] public $description;

#[Validate('required')] public $price;

public $observations; public $selectObservation;

public Treatment $treatment; //List of Observations public $listOb = []; when I comment on the definition and use of the variables observations and listOb everything starts working normally or the opposite when I remove my mutator.

0 likes
6 replies
jaseofspades88's avatar

This is markdown, so please wrap your code in backticks...

like me

Without further insights one can only assume that you're appending the accessor to your model and it's trying to resolve it incorrectly. This actually doesn't have anything to do with the validation.

I recommend you use the enum to encapsulate the label instead of adding the accessor anyway.

enum Type
{
	case CONTRA = 'contra';
	case SIDE_EFFECT = 'side-effect';

	public function display()
	{
		return match($this) {
			self::CONTRA => 'Contradindication',
			self::SIDE_EFFECT => 'Side effect',
		};
	} 
}

This assumes of course that you're casting the type field on the model.

cris_lml's avatar

@jaseofspades88 Sorry, I'm not very familiar with the platform, this is my code:

<?php

namespace App\Enums;

enum Type:int
{
    case CONTRAIN=0;
    case EFFECT=1;
}

This is where I think the problem is, but the weird thing is that I only get the error when I activate my input validation.

class Observation extends Model
{
    use HasFactory;
    public $fillable = [
        'name',
        'type'
    ];

    protected function type(): Attribute
    {
        return Attribute::make(
            get: fn(int $value) => $value==Type::CONTRAIN ? 'Contraindicacion' : 'Efecto Secundario'
        );
    }

    protected function casts(): array
    {
        return [
            'type' => Type::class
        ];
    }

}

This is the class of my component where I have some attributes with validation, but the attributes that do not have validation are being validated or something like that, because if I remove those attributes it also starts working normally.

class TreatmentForm extends Component
{
    //Tratamiento
    public $id;

    #[Validate('required')]
    public $name;

    #[Validate('required')]
    public $description;

    #[Validate('required')]
    public $price;

    public $observations; //If I remove this attribute the error disappears
    public $selectObservation;

    public Treatment $treatment;
    //Lista de Observaciones
    public $listOb = []; //If I remove this attribute the error disappears

    protected $listeners = ['modalEdit' => 'getTreatment'];

    public function getTreatment($id){
        $this->treatment = Treatment::find($id);
        $this->id = $this->treatment->id;
        $this->name = $this->treatment->name;
        $this->description = $this->treatment->description;
        $this->price = $this->treatment->price;
        $this->observations = $this->treatment->observations;
        $this->listOb = Observation::all();
    }
    public function updateOrCreate(){
        // $this->validate();
        $treatment = Treatment::updateOrCreate(
            [
                'id' => $this->id
            ],[
                'name' => $this->name,
                'description' => $this->description,
                'price' => $this->price
            ]
        );
        $this->getTreatment($treatment->id);
        $this->dispatch('reee');
    }

<div>
    <div class="input-group">
        <span class="input-group-text">Nombre</span>
        <input wire:model="name" type="text" class="form-control">
    </div>
    <div>
        @error('name')
        <span class="error">{{ $message }}</span>
        @enderror
    </div>
    <div class="input-group">
        <span class="input-group-text">Precio</span>
        <input wire:model="price" type="number" class="form-control">
    </div>
    <div>
        @error('price')
        <span class="error">{{ $message }}</span>
        @enderror
    </div>
    <div class="form-floating">
        <textarea class="form-control" wire:model="description"></textarea>
        <label>Descripción</label>
    </div>
    <div>
        @error('description')
        <span class="error">{{ $message }}</span>
        @enderror
    </div>
    <div class="mt-1" @if (!$id) hidden @endif>
        <div class="d-flex justify-content-between mb-1">
            <div class="d-flex align-items-end">
                <h5>Observaciones</h5>
            </div>
        </div>
        <div>
            <div class="input-group m-1">
                <select class="form-select" >
                    <option value="">Selecione una Observación</option>
                    @foreach ($listOb as $item)
                    <option value="{{$item->id}}" >{{$item->name}}</option>
                    @endforeach
                </select>
                <a class="btn btn-success" wire:click="addObservation">
                    <i class="fa fa-plus"></i>Añadir
                </a>
            </div>
        </div>
        <table class="table table-bordered table-striped">
            <thead>
                <tr>
                    <th>Nombre</th>
                    <th>Tipo</th>
                </tr>
            </thead>
            <tbody>
                @foreach ($observations ?? [] as $observation)
                <tr>
                    <td>{{ $observation->name }}</td>
                    <td>{{ $observation->type }}</td>
                    <td>
                        <a class="btn btn-danger" wire:click="deleteObservation({{$observation->id}})">
                            <i class="fa fa-trash"></i>
                        </a>
                    </td>
                </tr>
                @endforeach
            </tbody>
        </table>
    </div>
    <div class="modal-footer">
        <a wire:click="restart" class="btn btn-secondary" data-bs-dismiss="modal">Cancelar</a>
        @if (!$id)
        <a wire:click="updateOrCreate" class="btn btn-success">Nuevo Tratamiento</a>
        @else
        <a wire:click="" class="btn btn-danger" data-bs-dismiss="model">Eliminar</a>
        <a wire:click="updateOrCreate" class="btn btn-success" data-bs-dismiss="model">Guardar</a>
        @endif
    </div>
</div>

jlrdw's avatar
```
Code in here
```

Backticks above tab key.

JussiMannisto's avatar

The error page / stack trace contains a lot of more information than just the error message. That includes the filename and line number of the error's origin. Take a look there. Follow the stack trace if needed.

Btw, comparing backed enums to primitives won't work that way; $value == Type::CONTRAINT will always return false. You need to get the scalar value: $value == Type::CONTRAINT->value.

cris_lml's avatar

@JussiMannisto I checked the laravel.log and looked for the error from there, and I saw that mutators do not work with accessors or at least not with enums, regarding comparisons in the enum you are correct.

Please or to participate in this conversation.