F
Filamentβ€’3mo ago
daar

Conditional rules not working

Hi, I want to enable custom rules conditionally, but this is not being called. What am I missing here? I have attached the code used below.
Forms\Components\Select::make('identification_type')
->label(__('Identification type'))
->options(IdentificationType::class)
->live()
->required(), Forms\Components\TextInput::make('citizen_service_number')
->label(__('Identification number'))
->rules([
static fn (Get $get): Closure => static function (string $attribute, $value, Closure $fail) use ($get): void {
if ($get('identification_type') === IdentificationType::CEDULA->value) {
$cedula = new Cedula($value);
if ( ! $cedula->isValid()) {
$fail(__('The citizen service number is invalid'));
}
}
},
])
->maxLength(255),
Forms\Components\Select::make('identification_type')
->label(__('Identification type'))
->options(IdentificationType::class)
->live()
->required(), Forms\Components\TextInput::make('citizen_service_number')
->label(__('Identification number'))
->rules([
static fn (Get $get): Closure => static function (string $attribute, $value, Closure $fail) use ($get): void {
if ($get('identification_type') === IdentificationType::CEDULA->value) {
$cedula = new Cedula($value);
if ( ! $cedula->isValid()) {
$fail(__('The citizen service number is invalid'));
}
}
},
])
->maxLength(255),
7 Replies
Lara Zeus
Lara Zeusβ€’3mo ago
do you get the correct value when you 'dd($get(...))' inside the rules
daar
daarβ€’3mo ago
I get nothing, it does not seem to reach the code
Lara Zeus
Lara Zeusβ€’3mo ago
validation will work after you click save
daar
daarβ€’3mo ago
Thanks @Lara Zeus , but nothing happens after I click save unfortunately.
Lara Zeus
Lara Zeusβ€’3mo ago
this dose workd for me
Select::make('identification_type')
->options([1=>'one',2=>'one2',3=>'one3',])
->live()
->required(),
TextInput::make('citizen_service_number')
->rules([
static fn (Get $get): Closure => static function (string $attribute, $value, Closure $fail) use ($get): void {
if ($get('identification_type') == 1) {
$fail(__('The citizen service number is invalid'));
}
},
]),
Select::make('identification_type')
->options([1=>'one',2=>'one2',3=>'one3',])
->live()
->required(),
TextInput::make('citizen_service_number')
->rules([
static fn (Get $get): Closure => static function (string $attribute, $value, Closure $fail) use ($get): void {
if ($get('identification_type') == 1) {
$fail(__('The citizen service number is invalid'));
}
},
]),
today I am working on custom roles, so I was interested to check this out πŸ™‚ also dd() before the if condition works fine see if you got any other errors on console!
Lara Zeus
Lara Zeusβ€’3mo ago
No description
daar
daarβ€’3mo ago
Let me check! Thanks @Lara Zeus
Want results from more Discord servers?
Add your server