How to validate repeater items certain conditions before create - Attributes IN repeater items

Hello. Not possible with observer or with mutateFormDataBeforeCreate I have a form to create dossiers, and each dossier can have multiple interveners; I am using a repeater. I need to validate, for example, if there are 3 interveners and not all 3 have the field invoiceable == true then halt and prevent the user from saving the Dossier and make Notification. The fact is that if I use mutateFormDataBeforeCreate(array $data): array and I dd(data); I see no interveners array (no array for the repeater) and I cannot check. Also, using an Observer, in the creating function, attributes DO NOT include repeaters, so this always gives an empty array for the interveners repeater (hasMany RelationShip):
public function creating(Expediente $expediente): void
{
$cuentaIntervinientes= $expediente->intervinientes->count();
$cuentaIntervinientesFacturables= $expediente->intervinientes->where('facturable',true)->count();
dd($cuentaIntervinientesFacturables, $cuentaIntervinientes);
if ($cuentaIntervinientesFacturables == $cuentaIntervinientes && $cuentaIntervinientes == 1){
//
} elseif ($cuentaIntervinientesFacturables == 0){
...
throw new Halt();
} elseif ($cuentaIntervinientesFacturables > 1 && $cuentaIntervinientesFacturables < $cuentaIntervinientes){
Notification::make()
->title('No todos los intervinientes son facturables. O todos o sólo uno')
->body('El expediente ' . $expediente->numExp . ' tiene ' . $cuentaIntervinientesFacturables . ' intervinientes facturables de ' . $cuentaIntervinientes)
->warning()
->send();
throw new Halt();
}
}
public function creating(Expediente $expediente): void
{
$cuentaIntervinientes= $expediente->intervinientes->count();
$cuentaIntervinientesFacturables= $expediente->intervinientes->where('facturable',true)->count();
dd($cuentaIntervinientesFacturables, $cuentaIntervinientes);
if ($cuentaIntervinientesFacturables == $cuentaIntervinientes && $cuentaIntervinientes == 1){
//
} elseif ($cuentaIntervinientesFacturables == 0){
...
throw new Halt();
} elseif ($cuentaIntervinientesFacturables > 1 && $cuentaIntervinientesFacturables < $cuentaIntervinientes){
Notification::make()
->title('No todos los intervinientes son facturables. O todos o sólo uno')
->body('El expediente ' . $expediente->numExp . ' tiene ' . $cuentaIntervinientesFacturables . ' intervinientes facturables de ' . $cuentaIntervinientes)
->warning()
->send();
throw new Halt();
}
}
Any ideas of how I can validate repeaters counts and fields attributes inside the repeaters??? Thanks.
3 Replies
sandofgods
sandofgods3mo ago
Hello, you can use the filament validation and add your own custom validation rules please check: https://filamentphp.com/docs/3.x/forms/validation#custom-rules
Albert Lens
Albert Lens3mo ago
Yes, tks, but I haven't found a validation rule that allows => equal to 1 or equal to another field's value, for example. Or even validation where => this fieldA must be X if model.relationship.fieldB is less than model.relationship.fieldC Maybe it is there in the Official docs, but I have not been able to find anything similar. Any help on this would be highly appreciated.
sandofgods
sandofgods3mo ago
You can write your own validation rules from scratch, all the references on the link