1

I have a Formtype. In this Formtype I get over the options-Array in the buildForm function a key additionalName. I want to add this value to the FormType Name (in Symfony3 BlockPrefix). But how can I set this?

class AdultType extends AbstractType
{    
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $additionalName = $options['additionalName'];

        $builder
            ->add('account', TextType::class,array(
                'label' => 'account',
                'required' => false,
            ))

        ;
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'My\Bundle\WebsiteBundle\Model\Adult',
            'csrf_protection' => true,
            'cascade_validation' => true,
            'name' => ""
        ));
    }

    /**
     * @return string
     */
    public function getBlockPrefix()
    {
        //Here I need the $options['additionalName']
        return 'my_bundle_websitebundle_adult_'.$options['additionalName'];
    }

I tried allready to set a variable private $additionalName; on the top of the class, set it in the buildForm function and get access to it with $this->additionalName in the getBlockPrefix Function. But the value in getBlockPrefix is empty. SO I think the getBlockPrefix is called before the buildForm.

The Type is beeing called from another form:

$builder->add('adult', AdultType::class, array(
                        'additionalName' => $options['name']
                    ));

Thanks for any help!

1
  • 2
    What is the actual use case that you want to solve by dynamically changing the block prefix? Commented Nov 24, 2016 at 14:54

2 Answers 2

0

I suggest you to add a property to your FormType like that :

class AdultType extends AbstractType
{    
    protected $_additionalName;

    public function __construct($additionalName= ''){
        $this->_additionalName = $additionalName;
    }


    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $additionalName = $options['additionalName'];

        $builder
            ->add('account', TextType::class,array(
                'label' => 'account',
                'required' => false,
            ))

        ;
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'My\Bundle\WebsiteBundle\Model\Adult',
            'csrf_protection' => true,
            'cascade_validation' => true,
            'name' => ""
        ));
    }

    /*
     * @return string
     */
    public function getBlockPrefix()
    {
        //Here I need the $options['additionalName']
        return 'my_bundle_websitebundle_adult_'.$this->_additionalName;
    }

EDIT :

Arf.. I think it is only available for Symfony 2 before (2.8)..

Sign up to request clarification or add additional context in comments.

Comments

-1

Try this:

class AdultType extends AbstractType
{
    protected $_additionalName;

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $this->_additionalName = $options['additionalName'];

        $builder
            ->add('account', TextType::class,array(
                'label' => 'account',
                'required' => false,
            ))

        ;
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'My\Bundle\WebsiteBundle\Model\Adult',
            'csrf_protection' => true,
            'cascade_validation' => true,
            'name' => ""
        ));
    }

    /**
     * @return string
     */
    public function getBlockPrefix()
    {
        //Here I need the $options['additionalName']
        return 'my_bundle_websitebundle_adult_'.$this->_additionalName;
    }

    public function getName(){
        return $this->getBlockPrefix();
    }
}

getBlockPrefix isnt aware of $options['additionalName'], therefore you need a class property. And additional you can try with getName()

3 Comments

Doesn#t work, the variable "$this->_additionalName" is null when calling it in "getBlockPrefix".
I don't know if it works, but you can try to set the fully block name in $options array: $options['block_name'] = "my_bundle_websitebundle_adult__additional"
I think you can only set the block prefix hardcoded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.