Limiting validation rules

I have a single form with multiple sections. I declare the validation rules for each field via the ->rules() method. I would like to only process the validation rules for the section that the action button was pressed in. I have reasons for using ->rules() instead of Filaments helper methods like ->string(). In the following code, when the personal_action button is pressed, I want to validate name but not company_name. Or, when the business_action is pressed, I want to validate company_name but not name. Is there a way to accomplish with Filament?
public function form(Form $form): Form
{
return $form->schema([
Section::make('personal')
->schema([
TextInput::make('name')
->rules(['max:20', 'required']),
Actions::make([
Action::make('personal_action')
->action(function (Set $set) {
$this->form->getState();

doSomethingMagical();
}),
]),
]),
Section::make('business')
->schema([
TextInput::make('company_name')
->rules(['max:20', 'required']),
Actions::make([
Action::make('business_action')
->action(function (Set $set) {
$this->form->getState();

doSomethingMagical();
}),
]),
]),
]);
}
public function form(Form $form): Form
{
return $form->schema([
Section::make('personal')
->schema([
TextInput::make('name')
->rules(['max:20', 'required']),
Actions::make([
Action::make('personal_action')
->action(function (Set $set) {
$this->form->getState();

doSomethingMagical();
}),
]),
]),
Section::make('business')
->schema([
TextInput::make('company_name')
->rules(['max:20', 'required']),
Actions::make([
Action::make('business_action')
->action(function (Set $set) {
$this->form->getState();

doSomethingMagical();
}),
]),
]),
]);
}
1 Reply
christhompsontldr
christhompsontldrOP4mo ago
Resolved it with
->rules(['max:20', 'required'], fn (Get $get): bool => $get('section') === 'business_action')
->rules(['max:20', 'required'], fn (Get $get): bool => $get('section') === 'business_action')
and
Action::make('business_action')
->action(function (Set $set) {
$set('section', 'business_action');

$this->form->getState();

doSomethingMagical();
}),
Action::make('business_action')
->action(function (Set $set) {
$set('section', 'business_action');

$this->form->getState();

doSomethingMagical();
}),
Want results from more Discord servers?
Add your server