1
votes

I have 2 entities (A and B) with a Many to One relationship between them.

I create my form with the A entity and i use an entity field (dropdown list) to display the rows in the B entity. I use a query builder to filter them. If don't change the values in the list (ie. with ajax), everything is working fine.

But if I change dynamicly the values in the dropdown, when I submit the form I have this error "This value is invalid"

It's because the submitted value isn't included in the "array" returned by the query builder.

It seems that this validation is automatic in symfony for entity field (I don't use any asserts on this field). I'd like to get rid of this. But how ?

2
Please can you provide code samples? It seems to be a problem with binded events. Actually, if you change your dropdown list with ajax then you have to redefine the PRE_BIND form event. I can provide you more concise solution after viewing a bunch of your code.fsenart
PRE_BIND did the trick. thank you !gbxxx
Hello gbxxx. I am stuck with the exact same problem and would like to know if you could edit your answer to help me out by describing a little more how you did implement the PRE_BIND solution. Thank you very much in advance :)Marc

2 Answers

0
votes

To answer my question a bit more explicitly :

The PRE_BIND form event can be redefined with an event listener in the function BuildForm like this example :

$factory = $builder->getFormFactory();

$builder->addEventListener(FormEvents::PRE_BIND, function($event) use ($factory) {
        $form = $event->getForm();
        $case = $event->getData();
        $id = $case['id'];

        if ($case) {
            $form->remove('id');
            $form->add($factory->createNamed('hidden', 'id',$id, array()));
        }
});
0
votes

For Symfony 2.3 you need to add the auto_initialize = false and change the order of params:

$form->add($factory->createNamed('id', 'hidden', $id, array('auto_initialize' => false)));