Symfony CollectionType add data when building without entity

I'm actually rendering a form, but this one is dynamic, by dynamic i want to say that i generate the Form with an XSD and i dont save an entity but i save the data inside this for, also I generate a template using smarty and the data store in the form, and store the HTML generated for this template in my database.

This form is used to create a Newsletter, everything is fine for the creation, but now i have to save this one as a draft newsletter (ie the newsletter is editable and can be editate when the user want...).

So I build my form life for the creation but i passed the data to the type like this :

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

But when i'm using the 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',
    ),));

I passed a DynamicFormType::class , here is the code :

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';
    }
}

If someone have an idea to add data to the CollectionType when I'm building this one... Or, at another moments... Feel free to ask me for more code. Thanks !

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

上一篇: JQueryUI对话框大小

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