Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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';
}
}

 

like image 523
roger.vila Avatar asked Jun 01 '15 08:06

roger.vila


1 Answers

You must specify the type of your field before adding options

 $builder->add('name', 'text', array('label' => 'Your name')) 
like image 131
Med Avatar answered Oct 18 '22 04:10

Med