F
Filament9mo ago
John

Why are disabled() fields validated? (e.g. required())

I'm building a workflow app with multiple forms. Sometimes a user can send back to the previous step. In that case, I only want the user to fill in a reason text field, and use a custom form action to send it back. Not bothering with validation of the rest of the form. That all works fine, except for the validation part. I reckoned that if I disabled() the fields, they would skip validation. I believe that's how standard HTML forms work. But that doesn't seem the case in Filament. Can I skip validation in some other way?
2 Replies
awcodes
awcodes9mo ago
The require() method adds browser validation attributes to the field. You can circumvent this by using ->rules() instead to force server validation only. But the real question is why would a field be required and disabled? Sounds like requiredIf() would be more appropriate.
John
JohnOP9mo ago
It indeed does add browser validation using the required attribute, but browser validation is skipped when the component is also disabled. It's server validation that's skill kicking in, including a translated Fieldname X is required error message. The only difference between required() and rules('required') is that the latter doesn't give me the asterisk*. It still validates disabled fields. I couldn't get requiredIf() to work. But you lead me to another solution:
Toggle::make('return')->label('Return to previous step')
// ...
SomeField::make(..)->required(fn($get) => !$get('return'))
Toggle::make('return')->label('Return to previous step')
// ...
SomeField::make(..)->required(fn($get) => !$get('return'))
It's not very pretty to include that closure to every required field, but it's working. The whole form stays visible and editable, just the required validation is dropped when sending back.
Want results from more Discord servers?
Add your server