Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when trying to use embedded forms in Symfony2

Tags:

forms

symfony

I'm trying to Embed a Collection of Forms just as shown here - http://symfony.com/doc/current/cookbook/form/form_collections.html

I almost rewrite the code from there but I met two problems:

  1. FatalErrorException: Compile Error: Declaration of MyBundle\Form\Type\ExpenseType::setDefaultOptions() must be compatible with that of Symfony\Component\Form\FormTypeInterface::setDefaultOptions() in MyBundle\Form\Type\ExpenseType.php line 33

  2. form_start() function doesn't exist.

Do you have any ideas how to solve the first problem? Nothing helps :(

P.S. I'm not adding any code, because it's the same as in the book, I only changed the names (or at least I think so), I'll add any code if needed.

ExpenseType.php

<?php

namespace MyBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class ExpenseType extends AbstractType
{   
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', 'text',array(
            'label'  => ' '));

        $builder->add('description', 'textarea',array(
            'label'  => ' '));

        $builder->add('expenseVariants', 'collection', array('type' => new ExpenseVairantType()));

    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'MyBundle\Entity\Expense',
        ));
    }

    public function getName()
    {
        return 'expense';
    }
} 
like image 667
Faery Avatar asked Aug 01 '13 12:08

Faery


1 Answers

You're missing

use Symfony\Component\OptionsResolver\OptionsResolverInterface;

from your imports.

like image 155
Stuart Grimshaw Avatar answered Oct 21 '22 04:10

Stuart Grimshaw