Symfony:动态添加选择框(添加动态选择语言) [英] Symfony : Dynamic add select box ( Add Dynamically select Language )

查看:77
本文介绍了Symfony:动态添加选择框(添加动态选择语言)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索如何使用表单生成器来动态选择语言的系统。

I'm searching how to implement a system of choosing languages dynamically using form builder.

所以我们必须有两个html数组输入字段:name = languages [ ],第二个将是name = languges_level []

so we must have two html array inputs field : name="languages[]" and the second will be name="languges_level[]"

因此,该系统允许用户设置可以在其级别上使用的语言。

So this system allows the user to set the language which he can speak with his level on it.

用户可以在提交表单之前动态添加/删除语言。

The User can add/remove Language dynamically before he submits the Form.

问题:

1-表单级别:字段表单类型是什么?我必须添加2个表单字段,将它们组合起来以创建将存储在数据库中的结果数组。因此,这两个字段不会与ORM映射。

1- Form Level : what will be the field form type? I have to add 2 form fields which will be combined to create my result array which will be stored in the database. So this two field will be not mapped with ORM.

->add('langues', TYPEXXX:class) 
->add('langues_level', TYPEXXX:class)

3-树枝级别:应该我也会在树枝上做一些更改吗?

3- Twig Level: should i make some change in the twig as well?

那么我的情况下最好的解决方案是什么?

So what will be the best solution in my case?

我的第一次尝试是:

->add('languages', CollectionType::class, array(
    'entry_type'   => ChoiceType::class,
    'entry_options'  => array(
        'choices'  => array(
                        'Français' => 'Français',
                        'English'     => 'English',
                        'Italien'    => 'Italien',
                        'Espanish'    => 'Espanish',
        ),
        'label'      => ' ',
    ),
))
->add('language_levels', CollectionType::class, array(
    'entry_type'   => ChoiceType::class,
    'entry_options'  => array(
        'choices'  => array(
                        'Beginner' => 'Beginner',
                        'Medium'     => 'Medium',
                        'Good'    => 'Good',
        ),
        'label'      => ' ',
    ),
));

但是这不起作用,正如我在图片中提到的..谁有一个完美的解决方案plz?

but this don't work as I mentioned in the picture .. who had a perfect solution plz ?

推荐答案

我认为您需要表单集合


所以我们必须有两个html数组输入字段: name = langues []和
秒将是name = langes_level []

so we must have two html array inputs field : name="langues[]" and the second will be name="langes_level[]"

(..)

3-树枝级别:我也应该对树枝进行一些更改吗?

3- Twig Level: should i make some change in the twig as well?

(..)

4- Javascript级别,当在HTML中创建的输入是干净的
时,我可以进行开发。

4- Javascript Level, i can develop it when the inputs are clean created in the html.

不,不,不。如果您使用的是Symfony之类的框架,那么正确地描述数组输入字段的命名方式是不正确的。描述您的实体字段,描述您的表单字段,Symfony将为所有表单元素命名。 Symfony Forms将为您呈现和处理表单,因此(很有可能)不必理会表单元素名称的确切含义。

No, no and no. Describing how your 'array input fields' should be named exactly is not the right mentality if you're using a framework like Symfony. Describe your entity fields, describe your form fields and Symfony will give all form elements a name. Symfony Forms will render and handle the form for you, so there is (very likely) no need to be bothered what the form element names are exactly.

您的实体类:

class LanguageLevel
{
    protected $user;
    protected $language;
    protected $level;

    //getters and setters
}

创建表格类型:

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('languages', CollectionType::class, array(
            'entry_type' => LanguageLevelType::class,
            'allow_add' => true,
        ));
    }
}

和LanguageLevelType:

And a LanguageLevelType:

class LanguageLevelType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
        ->add('language', LanguageType::class)
        ->add('level', ChoiceType::class, array(
            'choices'  => array(
                'Good' => 5,
                'Bad' => 1,
             ),
        ));
    }
}

如果渲染的输出不是您想要的,请检查文档,如果您可以配置表单类型。可以为特定情况手动更改树枝模板,控制器和/或JavaScript,但我认为上面的表单集合将涵盖您的用例。

If the rendered output is not what you want, check the documentation if you can configure the Form Types. Manually changing the twig template, your controller and/or javascripts for a specific case is possible, but I think the 'Collection of Forms' from above will cover your use case.

这篇关于Symfony:动态添加选择框(添加动态选择语言)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆