0

这是我的文件:

<?php

namespace EM\ExpensesBundle\Entity;

use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\AbstractType;

class ChooseCatType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('name', 'entity', array(
            'class' => 'EMMyFriendsBundle:Category',
            'property' => 'name',
            'empty_value' => 'All items',
            'required' => false,
            'query_builder' => function ($repository)
                { return $repository->createQueryBuilder('cat')
                                    ->select('cat')
                                    ->orderBy('cat.name', 'ASC');
                }, ));
    }

    public function getName()
    {
        return 'choose_category';
    }
}

在这里,我创建了表单:

namespace EM\ExpensesBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use EM\ExpensesBundle\Entity\Category;
use EM\ExpensesBundle\Entity\ChooseCatType;

class HomeController extends Controller
{
    public function indexAction()
    {
        //Categories
        $cat = new Category();
        $dd_form = $this->createForm(new ChooseCatType(), $cat);
        return $this->render('EMExpensesBundle:Home:index.html.twig', array(
            'dd_form' => $dd_form->createView()));
    }
}

和模板:

{% extends "::base.html.twig" %}

{% block title %}
    Expenses
{% endblock %}

{% block body %}

    <div class="content">

<p> Choose category: </p>

            <form class="cat" action="" method="post" {{ form_enctype(dd_form) }}> 
                {{ form_widget(dd_form.name) }}
                {{ form_rest(dd_form) }}
                <input type="submit" value="Show items" />
            </form>

            <a href=""> Manage Categories </a>

        </div>

{% endblock %}

但我收到一个错误:

Fatal error: Declaration of EM\ExpensesBundle\Entity\ChooseCatType::buildForm() 
must be compatible with that 
of Symfony\Component\Form\FormTypeInterface::buildForm()     
in C:\xampp\htdocs\Expenses\src\EM\ExpensesBundle\Entity\ChooseCatType.php 
on line 9

有任何想法吗?

4

1 回答 1

1

FormBuilderInterface在您的方法签名中使用:

public function buildForm(FormBuilderInterface $builder, array $options)
于 2012-10-01T14:25:08.987 回答