F
Filament16mo ago
Abi

Conditional Validating a form with a repeater

I have a form with a select field and a repeater with 3 other fields. The select element is to select pre-existing data and the repeater is to add new data. How can I validate if one them has a value. The user can either pick a value from the select field or add a new item in the repeater or do both. What is a good way to validate something like this? I am using the standalone form builder and here is what I have
Action::make('addToCart')
->label('ADD PLAYERS TO CART')
->form([
Select::make('existing_kids')
->label('Select existing kids information previously added')
->multiple()->options($this->getFamilyMembers()),
Placeholder::make('or')->hiddenLabel()->content(new HtmlString(<<<HTML
<div class="relative">
<div class="absolute inset-0 flex items-center" aria-hidden="true">
<div class="w-full border-t border-gray-300"></div>
</div>
<div class="relative flex justify-center">
<span class="bg-white rounded-full px-2 text-sm text-gray-700">or</span>
</div>
</div>
HTML
)),
Repeater::make('add_new_kids')->schema([
TextInput::make('name')
->required()
->label('Full Name'),
TextInput::make('age')
->numeric()
->required(),
])->addActionLabel('Add a New Kid')
->label('Add New Kids')
->reorderable(false)
->columns(2),
])->action(fn()=>ray($this->form->getState()));
Action::make('addToCart')
->label('ADD PLAYERS TO CART')
->form([
Select::make('existing_kids')
->label('Select existing kids information previously added')
->multiple()->options($this->getFamilyMembers()),
Placeholder::make('or')->hiddenLabel()->content(new HtmlString(<<<HTML
<div class="relative">
<div class="absolute inset-0 flex items-center" aria-hidden="true">
<div class="w-full border-t border-gray-300"></div>
</div>
<div class="relative flex justify-center">
<span class="bg-white rounded-full px-2 text-sm text-gray-700">or</span>
</div>
</div>
HTML
)),
Repeater::make('add_new_kids')->schema([
TextInput::make('name')
->required()
->label('Full Name'),
TextInput::make('age')
->numeric()
->required(),
])->addActionLabel('Add a New Kid')
->label('Add New Kids')
->reorderable(false)
->columns(2),
])->action(fn()=>ray($this->form->getState()));
Not quite sure on how to validate the above login on the action method
12 Replies
Abi
AbiOP16mo ago
any advice on this is appreciated
Patrick Boivin
Patrick Boivin16mo ago
So if there is a value in the Select, the Repeater is not needed, and vice versa?
Abi
AbiOP16mo ago
Correct, there is also chances that they select something on the Select and add on the repeater if the value did not exist
Abi
AbiOP16mo ago
basically the form looks like this
Patrick Boivin
Patrick Boivin16mo ago
What should happen if both the Select and Repeater have values? Can this sometimes be valid or never?
Abi
AbiOP16mo ago
yes, that is valid too
Patrick Boivin
Patrick Boivin16mo ago
I'm not sure I understand what you want to validate then? lol maybe I'm just missing some context.
Abi
AbiOP16mo ago
if both the fields don't have values, it doesn't validate
Patrick Boivin
Patrick Boivin16mo ago
aaah yes, ok
Abi
AbiOP16mo ago
ok let me try that for the select field, how do I use the requiredWithout to indicate atleast 1 item in the repeater is required?
Patrick Boivin
Patrick Boivin16mo ago
Not sure actually... maybe this?
->required(fn ($get) => count($get('add_new_kids')) === 0),
->required(fn ($get) => count($get('add_new_kids')) === 0),
Want results from more Discord servers?
Add your server