symfony2中的简单形式

时间:2015-03-27 10:28:02

标签: forms symfony

我的简单表单有问题。我有一个名为JurisdictionUser()的类,我的类形式是JurisdictionUserNewType()。 如果我把下一个代码,它的工作正常:

$jurisdiction_user = new JurisdictionUser();
$form = $this->createFormBuilder($jurisdiction_user)
        ->add('name', 'text')
        ->add('email', 'text', array('required' => false))
        ->add('save', 'submit')
        ->getForm();

但是,如果我把下一个代码:

$jurisdiction_user = new JurisdictionUser();
$form = $this->createFormBuilder(new JurisdictionUserNewType(), $jurisdiction_user);

它给了我下一个错误:

ContextErrorException: Catchable Fatal Error: Argument 2 passed to Symfony\Bundle\FrameworkBundle\Controller\Controller::createFormBuilder() must be of the type array, object given, called in /home/sierra/dev_env/iyc-open010/src/Radmas/BackofficeAdminBundle/Controller/UserAdminController.php on line 119 and defined in /home/sierra/dev_env/iyc-open010/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php line 175

我需要帮助xD。我的班级JurisdictionUserNewType()。

    <?php
/**
 * Created by PhpStorm.
 * User: diego
 * Date: 15/01/14
 * Time: 16:21
 */

namespace Radmas\BackofficeAdminBundle\Form\Type;
use Radmas\BackofficeAdminBundle\Form\Transformer\CapitalToSmallLetterTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class JurisdictionUserNewType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $transformer = new CapitalToSmallLetterTransformer();

        $builder
            ->add('name', 'text');

        $builder->add(
            $builder->create('email', 'text', array('required' => false))
                ->addModelTransformer($transformer)
        );
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
           'data_class' => 'Radmas\Open010Bundle\Document\JurisdictionUser'
        ));
    }

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

1 个答案:

答案 0 :(得分:3)

你应该使用

$this->createForm(new JurisdictionUserNewType(), $jurisdiction_user)

在控制器中,而不是createFormBuilder。