在Symfony2中创建一个表单作为服务

时间:2022-10-25 16:42:47

What I'd like to be able to do from any controller is:

我想从任何控制器中做的是:

$register = $this->get('register_manager');
return $this->render(
    'AcmeUserBundle:Account:register.html.twig',
     array(
         'form' => $register->getRegistrationForm(),
         )
 );

And in my template

在我的模板

<form>
    {{ form_widget(form) }}
</form>

Here's how I have set up so far

这是我到目前为止的设置

In my Acme/UserBundle/Resources/config/services.yml I have

在我Acme / UserBundle /资源/ config /服务。yml我有

parameters:
    register_manager.class: Acme\UserBundle\Manager\RegisterManager

services:
    register_manager:
        class:     %register_manager.class%
        arguments: [@form.factory]

In RegisterManager.php I have

在RegisterManager。php我有

namespace Acme\UserBundle\Manager;

use Acme\UserBundle\Form\Type\RegistrationType;
use Symfony\Component\Form\FormFactoryInterface;

class RegisterManager
{

    protected $formFactory;

    public function __construct(FormFactoryInterface $formFactory)
    {
        $this->formFactory = $formFactory;
    }


    public function getRegistrationForm()
    {
        return $this->formFactory->createBuilder(new RegistrationType());
    }
}

And in Acme\UserBundle\Form\Type\RegistrationType I have:

在Acme\UserBundle\表单中,我有:

namespace Acme\UserBundle\Form\Type;

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

class RegistrationType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('username','text');
        $builder->add('email','email');
        $builder->add('password','password');
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'Acme\UserBundle\Entity\User',
        );
    }

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

I know the RegistrationType() works as I've had it in a controller. My problem is with setting up RegisterManager as a service, I can't get the right components in there and I'm not sure where to look.

我知道RegistrationType()可以在控制器中使用。我的问题是将RegisterManager设置为一个服务,我无法在其中获得正确的组件,我不确定应该在哪里查找。

1 个解决方案

#1


20  

You're almost there, it seems. To get a Form object from your service, you should use FormFactoryInterface::create() instead of FormFactoryInterface::createBuilder()

你似乎快到了。要从服务中获取表单对象,应该使用FormFactoryInterface::create()而不是FormFactoryInterface:::createBuilder()

The reason why $this->createForm() works in controllers is because every controller is extending the base controller, which happens to implement this method.

$this->createForm()在控制器中工作的原因是每个控制器都在扩展基控制器,而基控制器恰好实现了这个方法。

I have found my IDE's ability to link to specific Symfony files highly helpful and I suggest you use one, if you already aren't. There's also a git repository, which you can find here.

我发现我的IDE能够链接到特定的Symfony文件非常有用,我建议您使用一个,如果您还没有的话。还有一个git仓库,您可以在这里找到。

#1


20  

You're almost there, it seems. To get a Form object from your service, you should use FormFactoryInterface::create() instead of FormFactoryInterface::createBuilder()

你似乎快到了。要从服务中获取表单对象,应该使用FormFactoryInterface::create()而不是FormFactoryInterface:::createBuilder()

The reason why $this->createForm() works in controllers is because every controller is extending the base controller, which happens to implement this method.

$this->createForm()在控制器中工作的原因是每个控制器都在扩展基控制器,而基控制器恰好实现了这个方法。

I have found my IDE's ability to link to specific Symfony files highly helpful and I suggest you use one, if you already aren't. There's also a git repository, which you can find here.

我发现我的IDE能够链接到特定的Symfony文件非常有用,我建议您使用一个,如果您还没有的话。还有一个git仓库,您可以在这里找到。