如何使用Symfony2中的FormBuilder将表单的URL设置为POST?

Acy*_*yra 24 symfony

我在我的Controller中动态加载不同的表单类并在我的模板中显示它们.这很好,除了Symfony2文档显示手动将模板的路由添加到模板中.

<form action="{{ path('task_new') }}" method="post" {{ form_enctype(form) }}>
    {{ form_widget(form) }}

    <input type="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)

我需要在FormBuilder类中设置该表单操作 - POST路由(例如'task_new')根据我正在使用的表单类而不同.有没有办法在FormBuilder类中设置表单操作URL?我们怎样才能获得{{form_widget(form)}}来呈现完整的表单,而不仅仅是行?谢谢!

Pra*_*ush 47

它可以开箱即用 - http://symfony.com/doc/current/book/forms.html#changing-the-action-and-method-of-a-form

$form = $this->createFormBuilder($task)
->setAction($this->generateUrl('target_route'))
->setMethod('GET')
->add('task', 'text')
->add('dueDate', 'date')
->add('save', 'submit')
->getForm();
Run Code Online (Sandbox Code Playgroud)

  • 仅适用于控制器。 (2认同)

Tom*_*mek 32

我有同样的问题.我正在使用一个简单的FormType类,并希望在buildForm函数中设置动作URL .我尝试了不同的东西,但不能这样做.

最后我使用了一个名为"action"的Form选项.我不认为它是在Symfony Reference中记录的,我在阅读一些错误报告时偶然发现它:).您可以在控制器中创建表单时设置选项,如下所示:

$form = $this->createForm(new FormType(), $obj, array( 'action' => 'whatever you want'));
Run Code Online (Sandbox Code Playgroud)

它不像封装在表单类中那么漂亮,但它有效..我希望这会有所帮助.


小智 10

在表单类型中更改提交路由是不好的做法.它不构成类型责任.如果您从不处理表单路由添加表单,则只需在模板中更改操作URL:

  {{ form_start(yourForm,{action:path('yourSubmitRoute')}) }}
Run Code Online (Sandbox Code Playgroud)


Lay*_*son 9

我通过将路由器注入我的表单类型来解决了这个问题.在我的应用程序中,我创建了一个名为ZipCodeSearchType的邮政编码搜索表单:

表格类

use Symfony\Component\Form\AbstractType;
/*
 * I'm using version 2.6. At this time 2.7 has introduced a 
 * new method for the Option Resolver. Refer to the documentation 
 * if you are using a newer version of symfony.
 */
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Routing\Router;

/**
 * Class ZipCodeSearchType is the form type used to search for plans. This form type
 * is injected with the container service
 *
 * @package TA\PublicBundle\Form
 */
class ZipCodeSearchType extends AbstractType
{   
    /**
     * @var Router
     */
    private $router;

    public function __construct(Router $router)
    {
        //Above I have a variable just waiting to be populated with the router service...
        $this->router = $router;
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('zipCode', 'text', [
                'required' => true,
            ])
            /*
             * Here is where leverage the router's url generator
             */
            //This form should always submit to the ****** page via GET
            ->setAction($this->router->generate('route_name'))
            ->setMethod("GET")
        ;
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

下一步是将表单配置为服务,让symfony知道您需要将注册表服务注入到您的类中:

将表单定义为服务

/*
 * My service is defined in app/config/services.yml and you can also add this configuration
 * to your /src/BundleDir/config/services.yml
 */
services:
    ############
    #Form Types
    ############
    vendor_namespace.zip_search_form:
        class: VENDOR\BundleNameBundle\Form\ZipCodeSearchType
        arguments: [@router]
        tags:
            - { name: form.type, alias: zip_code_search }
Run Code Online (Sandbox Code Playgroud)

在您的控制器中使用它

/**
 * @param Request $request
 * @return Form
 */
private function searchByZipAction(Request $request)
{
    ...

    $zipForm = $this
        ->createForm('zip_code_search', $dataModel)
    ;
    ...
}
Run Code Online (Sandbox Code Playgroud)