0

所以我有一个搜索栏表单,我需要临时连接到一个遗留的非 symfony 页面。当前获取 url 如下所示(url-decoded)

http://localhost:9090/lagacy_page?query=test&platforms[]=Mac,Windows

但我需要使网址如下所示

http://localhost:9090/lagacy_page?query=test&platforms=Mac,Windows

Symfony 正在使平台成为一个数组,如果有办法强制它成为一个逗号分隔的列表,是否有人反对?

这是 buildForm 方法

/**
 * method to build search bar form
 *
 * @param \Symfony\Component\Form\FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    // the platform selector
    $builder->add('platform', 'choice',
        ['choices' => [
            Platforms::ALL => 'All Software', // TODO: need to translate this
            Platforms::WINDOWS => 'Windows',
            Platforms::MAC => 'Mac',
            Platforms::IOS => 'iOS',
            Platforms::ANDROID => 'Android',
        ],
        'multiple' => true,
        'expanded' => true]);

    // the actual search bar
    $builder->add('query', 'search');

}
4

2 回答 2

0

You will want to override how Symfony2 renders the choice field.

The documentation has plenty of information about how to customize Form rendering.

If only the choice type of search form needs this, you will need to create a custom type in order to avoid conflicts with the other forms of your website.

In short, if you override the choice type using the first doc and you do not use a custom type every choice type will use the same behavior (the one you will create for your search form) and you probably don't want that.

An easy alternative solution would be to apply a custom form_div_layout.html.twig file directly to the form object. There wouldn't be any conflicts with other forms as you would use a custom template just for the search form.

After reading the docs my answer will make more sense and you will be able to solve your problem.

于 2013-07-15T20:50:34.050 回答
0

您必须使用两个表单元素,因为 Symfony 以正确的方式进行操作(根据 HTML 规范)

/**
 * method to build search bar form
 *
 * @param \Symfony\Component\Form\FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    // the platform selector
    $builder->add('platform_choice', 'choice',
        ['choices' => [
            Platforms::ALL => 'All Software', // TODO: need to translate this
            Platforms::WINDOWS => 'Windows',
            Platforms::MAC => 'Mac',
            Platforms::IOS => 'iOS',
            Platforms::ANDROID => 'Android',
        ],
        'multiple' => true,
        'expanded' => true,
        'attr' => [
            'class' => 'platform-sorce'
        ])
    ->add('platform', 'hidden', [
        'attr' => [
            'class' => 'real-platform'
        ]
    ]);

    // the actual search bar
    $builder->add('query', 'search');
}

然后添加 JS 更新隐藏字段,因为“platform_choice”被禁用并且不会被发送。

$(function(){
     var $real_platform = $('.real-platform'),
         $platform_source = $('.platform-source');

     $platform_source.change(function(){
         $real_platform.val($(this).val().join(',');
     });

     $('#your-form").submit(function(){
         $platform_source.attr('disabled', true);

         return true;
     });
});
于 2013-07-16T09:37:01.367 回答