Symfony Form - Expected argument of type "string or Symfony\Component\Form\FormTypeInterface", "array" given

13,258

Solution 1

You must specify the type of your field before adding options

 $builder->add('name', 'text', array('label' => 'Your name')) 

Solution 2

For the Symfony version 3+ it should be ,

$builder->add('name', TextType::class, array('label' => 'Your name')) 
Share:
13,258

Related videos on Youtube

roger.vila
Author by

roger.vila

http://twitter.com/_rogervila

Updated on September 16, 2022

Comments

  • roger.vila
    roger.vila over 1 year

    I have created a form with doctrine. It works if I do not pass any option, like this:

    $builder
        ->add('name')
        ->add('password', 'password')
        ->add('password_repeat', 'password')
        ->add('email', 'email')
        ->add('save', 'submit')
    ;
    

    But, if I add an array with options as it says the docs (http://symfony.com/doc/current/book/forms.html#book-form-creating-form-classes), I get an error that says:

    Expected argument of type "string or Symfony\Component\Form\FormTypeInterface", "array" given

    This is the formtype created by doctrine:

    <?php
    
    namespace MainBundle\Form;
    
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
    
    class UserType extends AbstractType
    {
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
    
        $builder
            ->add('name') //if I put ->add('name', array('label' => 'Your name')) I get the error
            ->add('password', 'password')
            ->add('password_repeat', 'password')
            ->add('email', 'email')
            ->add('save', 'submit')
        ;
    
    }
    
    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'MainBundle\Entity\User'
        ));
    }
    
    /**
     * @return string
     */
    public function getName()
    {
        return 'mainbundle_user';
    }
    }
    

     

  • roger.vila
    roger.vila almost 9 years
    Thank you! I am new at symfony so I supose it was a dumb question, but you saved my day!
  • Med
    Med almost 9 years
    It's dumb only if you don't ask