8

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

 

2 Answers 2

9

You must specify the type of your field before adding options

 $builder->add('name', 'text', array('label' => 'Your name')) 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I am new at symfony so I supose it was a dumb question, but you saved my day!
It's dumb only if you don't ask
4

For the Symfony version 3+ it should be ,

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

Comments

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.