roger.vila roger.vila - 1 month ago 19
PHP Question

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


 

Med Med
Answer

You must specify the type of your field before adding options

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