how to hide a form field conditionally?
I have two Radio Buttons. And want to hide the Select field candidato if no a special radio option is clicked.
Select::make('puesto')
->searchable()
->label( __('Oferta'))
->options(JobOffer::all()
->where('job_offer_open', true)
->sortBy('title')
->pluck('title', 'id'))
->required(),
Radio::make('comparison_type')
->label( __('Tipo de comparación'))
->options([
'1with1' => __('1 Oferta con 1 Candidato'),
'1withAll' => __('1 Oferta con todos los Candidatos'),
])
->default('1withAll')
->required()
->inline(),
Components\ConditionalSelect::make('candidato')
->searchable()
->label( __('Candidato (solamente activos)'))
->options(Applicant::all()
->where('is_active', true)
->sortBy('full_name')
->pluck('full_name', 'id'))
->dependsOn('comparison_type', '1with1')
->required(),
Select::make('puesto')
->searchable()
->label( __('Oferta'))
->options(JobOffer::all()
->where('job_offer_open', true)
->sortBy('title')
->pluck('title', 'id'))
->required(),
Radio::make('comparison_type')
->label( __('Tipo de comparación'))
->options([
'1with1' => __('1 Oferta con 1 Candidato'),
'1withAll' => __('1 Oferta con todos los Candidatos'),
])
->default('1withAll')
->required()
->inline(),
Components\ConditionalSelect::make('candidato')
->searchable()
->label( __('Candidato (solamente activos)'))
->options(Applicant::all()
->where('is_active', true)
->sortBy('full_name')
->pluck('full_name', 'id'))
->dependsOn('comparison_type', '1with1')
->required(),
2 Replies
Did you check this section?
https://filamentphp.com/docs/2.x/forms/advanced#dependant-fields--components
Thanks @Leandro Ferreira 🙂 resolved:
Select::make('puesto')
->searchable()
->label( __('Oferta'))
->options(JobOffer::all()
->where('job_offer_open', true)
->sortBy('title')
->pluck('title', 'id'))
->required(),
Radio::make('comparison_type')
->label( __('Tipo de comparación'))
->options([
'1with1' => __('1 Oferta con 1 Candidato'),
'1withAll' => __('1 Oferta con todos los Candidatos'),
])
->default('1withAll')
->required()
->reactive()
->inline(),
Select::make('candidato')
->searchable()
->options(Applicant::all()
->where('is_active', true)
->sortBy('full_name')
->pluck('full_name', 'id'))
->hidden(fn (Closure $get) => $get('comparison_type') === '1withAll')
//->required()
->label( __('Candidato')),
Select::make('puesto')
->searchable()
->label( __('Oferta'))
->options(JobOffer::all()
->where('job_offer_open', true)
->sortBy('title')
->pluck('title', 'id'))
->required(),
Radio::make('comparison_type')
->label( __('Tipo de comparación'))
->options([
'1with1' => __('1 Oferta con 1 Candidato'),
'1withAll' => __('1 Oferta con todos los Candidatos'),
])
->default('1withAll')
->required()
->reactive()
->inline(),
Select::make('candidato')
->searchable()
->options(Applicant::all()
->where('is_active', true)
->sortBy('full_name')
->pluck('full_name', 'id'))
->hidden(fn (Closure $get) => $get('comparison_type') === '1withAll')
//->required()
->label( __('Candidato')),