Symfony CollectionType在没有实体的情况下添加数据

我实际上是渲染一个表单,但这是动态的,动态我想说,我用XSD生成表单,我不保存一个实体,但我保存在这里面的数据,我也使用smarty生成模板和数据存储在表单中,并将为此模板生成的HTML存储在我的数据库中。

这个表格用于创建一个通讯,创建一切都很顺利,但现在我必须将这个表格保存为草稿通讯(即通讯是可编辑的,并且可以在用户需要时进行编辑)。

所以我为创建构建了我的表单生命,但是我将这些数据传递给了这样的类型:

$builder->add($insideElement[0].'-'.$key, NumberType::class, array('required' => false,'data' => $dataAsArray[$key][$insideElement[0]] , 'attr' => array('class'=> $key.'-'.$insideElement[0]) , 'data' => 0,0));

但是当我使用CollectionType

$builder->add($key, CollectionType::class, array(
    'entry_type' => DynamicFormType::class,
    'entry_options' => array('data' => $arrayOfFieldType),
    'allow_add' => true,
    'allow_delete' => true,
    'prototype' => true,
    'attr' => array(
        'class' => 'my-selector',
    ),));

我传递了一个DynamicFormType::class ,这里是代码:

namespace CoreBundleForm;

use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use SymfonyComponentFormExtensionCoreTypeSubmitType;
use SymfonyComponentFormExtensionCoreTypeTextType;
use SymfonyComponentFormExtensionCoreTypeTextareaType;
use SymfonyComponentFormExtensionCoreTypeEmailType;
use SymfonyComponentFormExtensionCoreTypeCountryType;
use SymfonyComponentFormExtensionCoreTypeFileType;
use SymfonyComponentFormExtensionCoreTypeNumberType;

class DynamicFormType extends AbstractType {

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        if(isset($options['data'][0])){
            foreach($options['data'][0] as $key => $field) {
                $label = substr($key,0,strpos($key, "-"));
                if($field[0] == 'SymfonyComponentFormExtensionCoreTypeFileType') {
                    $builder->add($key, $field[0], array('data_class' => null, 'required' => false, 'label' => $label, 'attr' => array('style' => 'width: 75%;')));
                }
                elseif($field[0] == 'SymfonyComponentFormExtensionCoreTypeNumberType') {
                    $builder->add($key, $field[0], array('data' => 0.0, 'label' => $label));
                }
                else {
                    $builder->add($key, $field[0], array('data' => '', 'label' => $label));
                }
            }
        } else {
            foreach($options['data'] as $key => $field) {
                $label = substr($key,0,strpos($key, "-"));
                if($field[0] == 'SymfonyComponentFormExtensionCoreTypeFileType') {
                    $builder->add($key, $field[0], array('data_class' => null, 'required' => false, 'label' => $label, 'attr' => array('style' => 'width: 75%;')));
                }
                elseif($field[0] == 'SymfonyComponentFormExtensionCoreTypeNumberType') {
                    $builder->add($key, $field[0], array('data' => 0.0, 'label' => $label));
                }
                else {
                    $builder->add($key, $field[0], array('data' => '', 'label' => $label));
                }
            }
        }
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {

    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'corebundle_dynamic';
    }
}

如果有人有一个想法,当我构建这一个时,将数据添加到CollectionType ...或者,在另一个时刻......随意问我更多的代码。 谢谢 !

链接地址: http://www.djcxy.com/p/61463.html

上一篇: Symfony CollectionType add data when building without entity

下一篇: how to pass new jquery value to CI controller using ajax