How to embed a precharged collection of non-entity forms in symfony2
        Posted  
        
            by 
                metalvarez
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by metalvarez
        
        
        
        Published on 2013-11-01T15:37:38Z
        Indexed on 
            2013/11/01
            15:53 UTC
        
        
        Read the original article
        Hit count: 311
        
I want to embed a collection of precharged non-entity forms, here is the code, first is the parent form buildForm method.
public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder->add("example1")->add("example2");
    $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
        /*some logic to do before adding the collection of forms*/
        $form->add('aclAccess', 'collection', array(
            'type' => new ChildFormType(),
            'allow_add' => true,
            'mapped' => false,
            'data' => /* I dont know how to precharge a collection of non-entity forms*/
        ));
    });
}
now the child form
public function buildForm (FormBuilderInterface $builder, array $options) {
    $builder->add("test1", "text", array("read_only" => true, "data" => "test"));
    $builder->->add("test2", "choice", array(
        'choices'   => array('opt1' => 'Opt1', 'opt2' => 'Opt2'),
        'multiple'  => true,
        'expanded'  => true
    ));
}
so basicly i want to manage those child options in the test2 field as separated forms, each option group will depend on the value of the test1 field, i know this can be done by coding everythin in twig without form classes but i think having form classes its the best practice to run phpunit test, for maintainability, etc ...
© Stack Overflow or respective owner