F
Filament7mo ago
Marc

How to make a rule "in" of a multiple select

I have the following code:
Select::make('middlewares')
->translateLabel()
->required()
->multiple()
->default(['web'])
->options([
'web' => 'web',
'auth' => 'auth',
'guest' => 'guest',
'signed' => 'signed'
])->in('web,auth,guest,signed'),
Select::make('middlewares')
->translateLabel()
->required()
->multiple()
->default(['web'])
->options([
'web' => 'web',
'auth' => 'auth',
'guest' => 'guest',
'signed' => 'signed'
])->in('web,auth,guest,signed'),
When I submit the form I get the error: The middlewares field is not in the list of allowed values. In Laravel, to validate an array, you use middlewares.*, but I don't know how to do it in filament
8 Replies
Dennis Koch
Dennis Koch7mo ago
Did you try an actual array? Not a comma separated string
Marc
MarcOP7mo ago
same problem
Marc
MarcOP7mo ago
No description
Marc
MarcOP7mo ago
->in(['web', 'auth', 'guest', 'signed'])
->in(['web', 'auth', 'guest', 'signed'])
It works if the multiple is removed, but when adding the multiple it gives me that error
Dennis Koch
Dennis Koch7mo ago
Ah. Makes sense, because it's an array. Maybe that's a bug
Marc
MarcOP7mo ago
In Laravel, if it is a validation of an array, the field.* is used, but if filament does not support this, I will have to do a manual validation
->rule(fn (Select $component) => function (string $attribute, $value, Closure $fail) use($component){
$in = ['web', 'auth', 'guest', 'signed'];
foreach ($value as $tmp) {
if (!in_array($tmp, $in)) {
$fail(__('validation.in', ['attribute' => $component->getLabel()]));
}
}
})
->rule(fn (Select $component) => function (string $attribute, $value, Closure $fail) use($component){
$in = ['web', 'auth', 'guest', 'signed'];
foreach ($value as $tmp) {
if (!in_array($tmp, $in)) {
$fail(__('validation.in', ['attribute' => $component->getLabel()]));
}
}
})
Dan Harrin
Dan Harrin7mo ago
actually, dont we have nestedRecursiveRules() for this?
Marc
MarcOP7mo ago
I think the filament doesn't have support for this case, I also tried with
->validationAttribute('middlewares.*')
->validationAttribute('middlewares.*')
but it doesn't work
Want results from more Discord servers?
Add your server