表单 – Symfony2:1表单来编辑一个可翻译的实体

前端之家收集整理的这篇文章主要介绍了表单 – Symfony2:1表单来编辑一个可翻译的实体前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个可翻译的实体,使用doctrine2的可翻译行为.

我正在尝试构建一个如下所示的表单:

| French |English| Spanish |
+--+--------|       |---------+------------+
|                                          |
| name:  [___my_english_name___]           |
|                                          |
| title: [___my_english_title__]           |
|                                          |
+------------------------------------------+

Order:  [___1___]
Online: (x) Yes
        ( ) No

所以基本上都有订单&不可翻译的对象的在线属性,以及名称& title属性具有可翻译的行为.

如果我的图纸不清楚:表单包含一个1个选项卡,每个区域设置保存可翻译的字段.

我所遇到的问题是,默认情况下,Symfony2将表单绑定到一个实体,但是这个原则可翻译的行为会迫使我每个语言环境有一个实体.个人而言,学说行为是正确的(我喜欢它),但是我无法创建一个允许我在所有语言环境中编辑实体的表单 – 以相同的形式.

到目前为止,我的主要形式是:

namespace myApp\ProductBundle\Form;

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

/**
 * Form for the productGroup.
 */
class ProductType extends AbstractType
{
    /**
     * Decide what field will be present in the form.
     *
     * @param FormBuilder $builder FormBuilder instance.
     * @param array       $options Custom options.
     *
     * @return null;
     */
    public function buildForm(FormBuilder $builder,array $options)
    {
        //Todo: get the available locale from the service.
        $arrAvailableLocale = array('en_US','en_CA','fr_CA','es_ES');

        //Render a tab for each locale
        foreach ($arrAvailableLocale as $locale) {
            $builder->add(
                'localeTab_' . $locale,new ProductLocaleType(),array('property_path' => false,//Do not map the type to an attribute.
                     ));
        }


        //Uni-locale attributes of the entity.
        $builder
            ->add('isOnline')
            ->add('sortOrder');


    }

    /**
     * Define the defaults options for the form building process.
     *
     * @param array $options Custom options.
     *
     * @return array Options with the defaults values applied.
     */
    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'myApp\ProductBundle\Entity\Product',);
    }

    /**
     * Define the unique name of the form.
     *
     * @return string
     */
    public function getName()
    {
        return 'myapp_productbundle_producttype';
    }
}

和表单:

<?PHP

namespace MyApp\ProductBundle\Form;

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

use invalidArgumentException;

/**
 * Form for the productGroupLocale tabs.
 */
class ProductLocaleType extends AbstractType
{
    /**
     * Decide what field will be present in the form.
     *
     * @param FormBuilder $builder FormBuilder instance.
     * @param array       $options Custom options.
     *
     * @return null;
     */
    public function buildForm(FormBuilder $builder,array $options)
    {


        $builder->add('name','text',array('data' => ???));
        $builder->add('title',array('data' => ???));

    }

    /**
     * Define the defaults options for the form building process.
     *
     * @param array $options Custom options.
     *
     * @return array Options with the defaults values applied.
     */
    public function getDefaultOptions(array $options)
    {
        return array(
            //'data_class' => 'MyApp\ProductBundle\Entity\Product','name' =>  '','title' => '',);
    }

    /**
     * Define the unique name of the form.
     *
     * @return string
     */
    public function getName()
    {
        return 'myapp_productbundle_productlocaletype';
    }
}

但是如你所未见的那样,我不知道如何从翻译的实体中获取名称标题值,而且一旦表单被提交,我也不知道如何坚持下去.

解决方法

嗨,如果你使用 gedmo extensions可翻译不是要处理多个翻译每个请求.尝试使用 knplabs alternative可能是一个更好的选择,以更一般的方式处理它.
原文链接:https://www.f2er.com/html/229714.html

猜你在找的HTML相关文章