Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Troubles with symfony form

I have the following route:

blog_show:
    path:      /test/123
    defaults:  { _controller: TotalcanBravofillBundle:Test:test }

And this controller:

    public function testAction()
    {
        $form = $this->createForm(new TestType(), new Test());

        return $this->render('TotalcanBravofillBundle:Test:test.html.twig', array(
            'form' => $form->createView(),
       ));
    }

I have this entity:

...
class Test
{
/**
     * @var string
     *
     * @ORM\Column(name="txt", type="text")
     */
    private $txt;
...

And this form:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('int')
            ->add('txt')
        ;
    }

I have this view:

{% extends "TotalcanBravofillBundle::index.html.twig" %}

{% block content %}
    {{ form_errors(form) }}
    {{ form_widget(form) }}
{% endblock %}

And it doesn't work; I get the following error:

Could not load type "text"
500 Internal Server Error - InvalidArgumentException

What am I doing wrong?

Stack trace:

at FormRegistry ->getType ('text') 
in /var/www/total1/data/bravofill/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php at line 82   + 
at FormFactory ->createNamedBuilder ('txt', 'text', null, array('required' => true)) 
in /var/www/total1/data/bravofill/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php at line 126   + 
at FormFactory ->createBuilderForProperty ('Totalcan\BravofillBundle\Entity\Test', 'txt', null, array()) 
in /var/www/total1/data/bravofill/vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php at line 109   + 
at FormBuilder ->create ('txt', null, array()) 
in /var/www/total1/data/bravofill/vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php at line 270   + 
at FormBuilder ->resolveChildren () 
in /var/www/total1/data/bravofill/vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php at line 218   + 
at FormBuilder ->getForm () 
in /var/www/total1/data/bravofill/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php at line 39   + 
at FormFactory ->create (object(TestType), object(Test), array()) 
in /var/www/total1/data/bravofill/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php at line 163   + 
at Controller ->createForm (object(TestType), object(Test)) 
in /var/www/total1/data/bravofill/src/Totalcan/BravofillBundle/Controller/TestController.php at line 14   + 
at TestController ->testAction () 
at call_user_func_array (array(object(TestController), 'testAction'), array()) 
in kernel.root_dir/bootstrap.php.cache at line 2774   + 
at HttpKernel ->handleRaw (object(Request), '1') 
in kernel.root_dir/bootstrap.php.cache at line 2748   + 
at HttpKernel ->handle (object(Request), '1', true) 
in kernel.root_dir/bootstrap.php.cache at line 2878   + 
at ContainerAwareHttpKernel ->handle (object(Request), '1', true) 
in kernel.root_dir/bootstrap.php.cache at line 2179   + 
at Kernel ->handle (object(Request)) 
in /var/www/total1/data/bravofill/web/app_dev.php at line 28   + 

Debug:

form.type.birthday   birthday   container Symfony\Component\Form\Extension\Core\Type\BirthdayType
form.type.button     button     container Symfony\Component\Form\Extension\Core\Type\ButtonType
form.type.checkbox   checkbox   container Symfony\Component\Form\Extension\Core\Type\CheckboxType
form.type.choice     choice     container Symfony\Component\Form\Extension\Core\Type\ChoiceType
form.type.collection collection container Symfony\Component\Form\Extension\Core\Type\CollectionType
form.type.country    country    container Symfony\Component\Form\Extension\Core\Type\CountryType
form.type.currency   currency   container Symfony\Component\Form\Extension\Core\Type\CurrencyType
form.type.date       date       container Symfony\Component\Form\Extension\Core\Type\DateType
form.type.datetime   datetime   container Symfony\Component\Form\Extension\Core\Type\DateTimeType
form.type.email      email      container Symfony\Component\Form\Extension\Core\Type\EmailType
form.type.entity     entity     container Symfony\Bridge\Doctrine\Form\Type\EntityType
form.type.file       file       container Symfony\Component\Form\Extension\Core\Type\FileType
form.type.form       form       container Symfony\Component\Form\Extension\Core\Type\FormType
form.type.hidden     hidden     container Symfony\Component\Form\Extension\Core\Type\HiddenType
form.type.integer    integer    container Symfony\Component\Form\Extension\Core\Type\IntegerType
form.type.language   language   container Symfony\Component\Form\Extension\Core\Type\LanguageType
form.type.locale     locale     container Symfony\Component\Form\Extension\Core\Type\LocaleType
form.type.money      money      container Symfony\Component\Form\Extension\Core\Type\MoneyType
form.type.number     number     container Symfony\Component\Form\Extension\Core\Type\NumberType
form.type.password   password   container Symfony\Component\Form\Extension\Core\Type\PasswordType
form.type.percent    percent    container Symfony\Component\Form\Extension\Core\Type\PercentType
form.type.radio      radio      container Symfony\Component\Form\Extension\Core\Type\RadioType
form.type.repeated   repeated   container Symfony\Component\Form\Extension\Core\Type\RepeatedType
form.type.reset      reset      container Symfony\Component\Form\Extension\Core\Type\ResetType
form.type.search     search     container Symfony\Component\Form\Extension\Core\Type\SearchType
form.type.submit     submit     container Symfony\Component\Form\Extension\Core\Type\SubmitType
form.type.textarea   textarea   container Symfony\Component\Form\Extension\Core\Type\TextareaType
form.type.time       time       container Symfony\Component\Form\Extension\Core\Type\TimeType
form.type.timezone   timezone   container Symfony\Component\Form\Extension\Core\Type\TimezoneType
form.type.url        url        container Symfony\Component\Form\Extension\Core\Type\UrlType
like image 238
Barif Avatar asked Feb 16 '23 20:02

Barif


1 Answers

when building your form you are not declaring any mappings of your entity to fields: http://symfony.com/doc/current/book/forms.html#building-the-form

this should work:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('int', 'int')
        ->add('txt', 'text')
    ;
}

I'd highly advise against renaming Symfony2 field mappings as in the other answer - your code will break everytime you upgrade Symfony.

like image 61
stefancarlton Avatar answered Feb 22 '23 23:02

stefancarlton