forms - Symfony2 : 1 form to edit a translatable entity

标签 forms symfony doctrine-orm translation

我有一个使用教义2的可翻译行为的可翻译实体。

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

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

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

所以基本上,对象的 order 和 online 属性是不可翻译的,而 name 和 title 属性具有可翻译的行为。

如果我的绘图不清楚:表单包含每个区域设置的 1 个选项卡,其中包含可翻译的字段。

我遇到的问题是,默认情况下,Symfony2 将一个表单绑定(bind)到一个实体,但是教义可翻译行为迫使我每个语言环境都有一个实体。就个人而言,教义行为很好(我喜欢它),但我无法制作允许我在所有语言环境中编辑实体的表单——以相同的形式。

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

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', 'text', 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 Translatable 并不是要处理每个请求的多个翻译。尝试使用 knplabs alternative以更一般的方式处理它可能是更好的选择。

关于forms - Symfony2 : 1 form to edit a translatable entity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8154379/

相关文章:

forms - 的表单例份验证。 NET 站点使用 Perl

php - Gedmo Timestampable(更新创建时)

php - Symfony - 渲染模板期间抛出异常

php - symfony2 mysql 基于时间间隔的排名

javascript - js函数中的多个if/else语句用于唯一输入禁用

javascript - 外部 Javascript 中 JQuery 调用的范围问题?

jquery - 为什么 <button> 也会触发表单提交

mysql - 复杂的 MySQL 左连接查询

php - Doctrine2 更新导致 Zend Framework 3 中 AnnotationRegistry registerLoader 错误

Symfony2 : do not update a form field if not provided