【问题标题】:Symfony Form choices customize the get urlSymfony 表单选项自定义获取 url
【发布时间】:2013-07-16 22:26:36
【问题描述】:

所以我有一个搜索栏表单,我需要临时连接到旧的非 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');

}

【问题讨论】:

  • 如何将这些平台保存在数据库中?你有一个 ENUM 或一个平台表吗?

标签: forms symfony get symfony-2.1


【解决方案1】:

您将需要覆盖 Symfony2 呈现选择字段的方式。

文档中有大量关于 how to customize Form rendering 的信息。

如果只有选择类型的搜索表单需要这个,您需要create a custom type以避免与您网站的其他表单发生冲突。

简而言之,如果您使用第一个文档覆盖 choice 类型并且您不使用自定义类型,则每个 choice 类型将使用相同的行为(您将为搜索表单创建的行为)并且您可能不想这样。

一个简单的替代解决方案是将自定义form_div_layout.html.twig 文件直接应用于表单对象。与其他表单不会有任何冲突,因为您会为搜索表单使用自定义模板。

阅读文档后,我的回答会更有意义,您将能够解决您的问题。

【讨论】:

    【解决方案2】:

    你必须使用两个表单元素,因为 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;
         });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-29
      • 1970-01-01
      相关资源
      最近更新 更多