How to hide one field in repeater form ( only in first element )??

hide one field in first form
25 Replies
toeknee
toeknee15mo ago
->hidden()
mathan7136
mathan7136OP15mo ago
Yes but, i only need to hide in parent element, i need that element in other repeater form !!
toeknee
toeknee15mo ago
So are you reusing the form? So you mean in the first of the repeater elements?
mathan7136
mathan7136OP15mo ago
Yes
toeknee
toeknee15mo ago
Tricky, you could use getter/setters and set rendered = true, then when adding an additional, check if rendered = true to hidden
mathan7136
mathan7136OP15mo ago
Is any example for this?? in doc?
toeknee
toeknee15mo ago
Not to hand, but a standard closure with $get and $set. Handling it on the repeater field it's self after state updated. Provide your code and I'll chuck something that might work onto it
mathan7136
mathan7136OP15mo ago
public function form(Form $form): Form { $formSpecificComponents = [ Forms\Components\Grid::make(3) ->schema( [ Forms\Components\TextInput::make('firstname') ->required() ->label('Firstname') ->required() ->maxLength(255) ->live(), Forms\Components\TextInput::make('middlename') ->label('Middlename') ->maxLength(255), Forms\Components\TextInput::make('lastname') ->required() ->label('Lastname') ->maxLength(255) ->live(), Forms\Components\DatePicker::make('date_of_birth') ->required(), Forms\Components\Select::make('meta.country_of_birth') ->options(Country::all()->pluck('name', 'id')) ->searchable() ->required(), Forms\Components\TextInput::make('meta.town_of_birth') ->maxLength(255) ->required(), Forms\Components\Toggle::make('primary') ->label('Primary Applicant'), ] ), ]; return $form->schema($this->baseFormSchema($formSpecificComponents)); } Any updates sir
toeknee
toeknee15mo ago
Where is your repeeater? That's a single form?
mathan7136
mathan7136OP15mo ago
its called from here sir protected function baseFormSchema(array $repeaterComponents): array { return [ $this->descriptionPlaceholder(), Repeater::make('applicants') ->relationship() ->schema($repeaterComponents) ->mutateRelationshipDataBeforeSaveUsing(function (array $data, Model $record): array { // Check if 'meta' exists in both $data and $record if (isset($data['meta']) && isset($record->meta)) { // Merge the 'meta' data from the record with the new data $data['meta'] = array_merge_recursive($record->meta, $data['meta']); } return $data; }) ->itemLabel(fn (array $state) => "{$state['firstname']} {$state['lastname']}"), ]; }
toeknee
toeknee15mo ago
What field are you trying to only show in the first form?
mathan7136
mathan7136OP15mo ago
primary sir ! trying to hide in first form sir !!
toeknee
toeknee15mo ago
Why hide it when it's a toggle. but just do something like this:
Forms\Components\Toggle::make('primary')
->label('Primary Applicant')
->hidden(fn(Closure $get) => count($get('../../applicants')) > 1)
Forms\Components\Toggle::make('primary')
->label('Primary Applicant')
->hidden(fn(Closure $get) => count($get('../../applicants')) > 1)
mathan7136
mathan7136OP15mo ago
i got following error sir App\Filament\Frontend\Resources\ApplicationResource\Pages\Edit\Applicants::App\Filament\Frontend\Resources\ApplicationResource\Pages\Edit{closure}(): Argument #1 ($get) must be of type Closure, Filament\Forms\Get given, called in /Users/ranium/Desktop/sites/projects/caseflow-main/vendor/filament/support/src/Concerns/EvaluatesClosures.php on line 35
awcodes
awcodes15mo ago
change 'Closure' to '\Filament\Forms\Get' 🙂
mathan7136
mathan7136OP15mo ago
In this condition i need to add '<', after i add this ->hidden(fn(\Filament\Forms\Get $get) => count($get('../../applicants')) == 1),its working but when i click add another its showing in both parent and child forms if it graeter than one i need to show in child fotm not in parent form sir i tried this but when toggle second fom's toggle element then the first form toggle also visible problem!! Forms\Components\Toggle::make('primary') ->label('Primary Applicant') ->hidden(function() { static $position = 0; $position++; if($position === 1) { return true; } }), Any ideas Sir?
awcodes
awcodes15mo ago
Can you provide a screen shot. I could be getting confused on what exactly you’re trying to accomplish.
mathan7136
mathan7136OP15mo ago
In this repeater i need to hide primary field ( only in first form of the repeater )
No description
mathan7136
mathan7136OP15mo ago
i tried this also but when i click primary in seconf form, the first one's primary automatically visibled ->hidden(function() { static $position = 0; $position++; if($position == 1) { return true; } }),
awcodes
awcodes15mo ago
I’ll dig some when I get to work. Hard to evaluate code on the phone.
mathan7136
mathan7136OP15mo ago
okay sir
thiktak
thiktak15mo ago
Is it a possible solution ? Could not work with complex objets (I have a nested component (some hints for that: the the current state, and rebuild the path ... and get the data before the search))
No description
No description
thiktak
thiktak15mo ago
Here the tips, is to get the state with all uuid generated [..., uuid => [type => type, data => []], and fetch the actual uuid position
mathan7136
mathan7136OP15mo ago
when i using this code. ->hidden(function($component) { $uuid = collect(explode('.', $component->getStatePath())) ->slice(0, -2) ->pop(); return collect($this->getFormStatePath())->keys()->search($uuid) !== 0; }), primary hidden permanently !!!, i need to hide only in first element my results, dd(uuid) = applicants
mathan7136
mathan7136OP15mo ago
dd($component) results in the image
No description
Want results from more Discord servers?
Add your server