How to hide one field in repeater form ( only in first element )??
hide one field in first form
25 Replies
->hidden()
Yes but, i only need to hide in parent element, i need that element in other repeater form !!
So are you reusing the form?
So you mean in the first of the repeater elements?
Yes
Tricky, you could use getter/setters and set rendered = true, then when adding an additional, check if rendered = true to hidden
Is any example for this??
in doc?
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
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 sirWhere is your repeeater? That's a single form?
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']}"),
];
}
What field are you trying to only show in the first form?
primary sir !
trying to hide in first form sir !!
Why hide it when it's a toggle.
but just do something like this:
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
change 'Closure' to '\Filament\Forms\Get' 🙂
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?
Can you provide a screen shot. I could be getting confused on what exactly you’re trying to accomplish.
In this repeater i need to hide primary field ( only in first form of the repeater )
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;
}
}),
I’ll dig some when I get to work. Hard to evaluate code on the phone.
okay sir
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))
Here the tips, is to get the state with all uuid generated [..., uuid => [type => type, data => []], and fetch the actual uuid position
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
dd($component) results in the image