Question

I have created a custom field type that has by default 1 field. Once that fields value is selected an event listener has subscribed to that field and is fired. If the value of that field matches an arbitrary value i have declared then another field is suppose to be added to the form.

The problem is that the event gets fired and i can debug to before and after adding the fields in the event listener however the new fields are not rendered when the form is returned.

Here is a simplified version of what im trying to do. Yes, I do have the custom field type registered properly as a service.

Main Form type

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('first_name','text')
       ->add('custom_field','my_custom_fields');
}

Then the custom type:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('field_1','entity',array(/*My entity settings*/));

    $formModifier = function(FormInterface $form, $campaign) use($options) {
        $form->add('field_2', 'entity',array(/*my entity options*/))
        ->add('Save','submit',array('attr'=>array('class'=>'btn btn-primary')));
    };

    $builder->addEventListener(
        FormEvents::PRE_SET_DATA,
        function(FormEvent $event) use ($formModifier, $options) {
            // this would be your entity
            $entity = $event->getData();
            //var_dump($entity); exit; // This gets Hit
            if(!$entity || !($entity instanceof My\Entity\Class) ){return;}
                $formModifier($event->getForm(), $entity);
            }
        }
    );

    $builder->get('field_1')->addEventListener(
        FormEvents::POST_SUBMIT,
        function(FormEvent $event) use ($formModifier, $options) {
            $data = $event->getForm()->getData();
            //var_dump($data); exit; //This gets hit
            if(null === $data){ return;}
            //var_dump($data); exit; // This also gets hit
            $formModifier($event->getForm()->getParent(),$data);
            //var_dump($event->getForm()->getParent()); exit; This also gets hit, AND FIELDS ARE PRESENT.
        }
    );

}
public function getName()
{
    return 'my_custom_fields';
}

But when i try and update this form:

public function updateFormAction(Request $request, MyEntityClass $entity)
{
    $form = $this->createForm(new MyMainFormType(), $entity);
    if ($request->isMethod('post')) {
        $form->bind($request);
        if($form->isValid()) {
            return new JsonResponse(array(
                'status': 'success',
                'html': $this->renderView('my_twig_template.html.twig', array(
                    'form'=>$form->createView()
                ))
            ), 200);
        }
    }
}

Then my_twig_template.html.twig:

{{form(form)}}

I have javascript listening on change of the fields and that will do an ajax call and try and update the form. But no matter what data i pass even though the correct events are being called and the form modification happens when the form is rendered only the first field for my_custom_field type is rendered.

Any Help will be greatly appreciated as to why this is happening.

Was it helpful?

Solution

The issue had to do with data_class for the new form. Once I added a new model that had the 2 fields and set a data_class pointing to that new model the fields showed up.

Main Form type

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('first_name','text')
       ->add('custom_field','my_custom_fields',array(
           'data_class' => 'Namespace\Bundle\Form\Type\Model\MyCustomFieldsClass'
       ));
}

Then just a simple class:

<?php
namespace Namespace\Bundle\Form\Type\Model;

class MyCustomFieldsClass
{
    public field_1;

    public field_2;

    /** getters & setters **/
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top