FilamentF
Filament3y 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()));


Not quite sure on how to validate the above login on the action method
Was this page helpful?