I have one form. I wish to give to users option to set some of its fields as required. Is there good way to do this?
I tried to get all fields of form, but can't find fields of sub-elements.
class DemandType extends AbstractType
{
...
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('client', ChoiceType::class, [
'label' => 'Client',
'attr' => [
'class' => 'form-control-sm',
],
'label_attr' => [
'class' => 'label-form',
],
])
...
->add('demandFiles', CollectionType::class, [
'entry_type' => DemandFileType::class,
'allow_add' => true,
'by_reference' => false,
'required' => false,
])
}
Controller:
$demand = new Demand();
$formDemand = $this->createForm(DemandType::class, $demand);
$arrayDemandFields = [];
foreach ($formDemand as $childFormDemand) {
$stringLabel = $childFormDemand->getConfig()->getOption('label');
$arrayDemandFields[$childFormDemand->getName()] = $stringLabel;
}
var_dump($arrayDemandFields);
returns
['client'=>'Client', 'demandFiles' => null]
How to fix it? Or maybe there is another, better way to do this?
Thanks for your help.