@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>