简体   繁体   中英

Is there a way to add a form field depending on a condition on Symfony?

Friends. This is the first question I ask here; so, any feedback will be welcome.

I'm new to Symfony; the version in which I'm working is 3.4 I already implemented three Type classes for my form and the structure is something like this: There is a Registry which has a header plus zero, one or more questions. Each question is a "RegistryElement" which consist on zero, one or more answers plus an "elementScore" and an optional "elementComment."

The Type classes buildForm methods look like this:

On RegistryType.php:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('registryHeaderId', RegistryHeaderType::class, array(
            'label' => false,
        ))
        ->add('registryElements', CollectionType::class, array(
            'label'=>false,
            'entry_type' => RegistryElementType::class,
            'entry_options' => [
                'label' => false,
                'tokens'=>$options['tokens'],
            ],
            'allow_add' => true,
            'by_reference' => false,
        ))
        ->add('save', SubmitType::class, array("attr"=>array(
            "class"=>"form-submit btn-success",
            "style"=>"margin:10px;"
        )))
        ;

}

On RegistryElementType.php:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('registryElementAnswers', CollectionType::class, array(
            'label'=>false,
            'entry_type' => RegistryElementAnswerType::class,
            'entry_options' => [
                'label' => false,
            ],
            'allow_add' => true,
            'by_reference' => false,
        ))
        ->add('elementScore', CheckboxType::class, array(
            "label"=>'type.registry_element_type.score',
            'required' => false,
            "translation_domain" => "FirstPieceBundle",
        ))
        ->add('elementComment', TextType::class, array(
            "label"=>'type.registry_element_type.element_comment',
            "required"=>false,
            "attr"=>array(
                "class"=>"form-name form-control",
            ),
            "translation_domain" => "FirstPieceBundle",
        ))
        ;

}

And, on RegistryElementAnswerType.php:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('answer', TextType::class, array(
            "label"=>false,
            "required"=>false,
            "attr"=>array(
                "class"=>"form-name form-control",
            ),
            "translation_domain" => "FirstPieceBundle",
        ))
        ;

}

Now, what I would like to know is if there is a way to "add a different type of field" depending on a condition. Something like this:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    if(condition){
        $builder
            ->add('answer', TextType::class, array(
                "label"=>false,
                "required"=>false,
                "attr"=>array(
                    "class"=>"form-name form-control",
                ),
                "translation_domain" => "FirstPieceBundle",
            ))
            ;
    }
    else{
        $builder
            ->add('answer', ChoiceType::class, array(
                "label"=>false,
                "required"=>false,
                "attr"=>array(
                    "class"=>"form-name form-control",
                ),
                "translation_domain" => "FirstPieceBundle",
            ))
            ;
    }

}

I tried to pass some values through the $options array, but I really got stuck here. I'd really appreciate your knowledge and guidance.

Greetings.

What exactly is your condition you need to use there?

If you want to pass data in the $options array, you will need to add the option key as a required option in the configureOptions method.

Let's say you want to pass condition in the $options , your RegistryElementAnswerType class should contain:

public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setRequired('condition');
    }

Now you are required to pass this option whenever you are referring to the RegistryElementAnswerType .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM