Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 ?

share|improve this question
    
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 Jul 11 '12 at 14:46
2  
PRE_BIND did the trick. thank you ! –  gbxxx Jul 12 '12 at 7:06
    
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 Jan 22 '13 at 11:47

2 Answers 2

up vote 0 down vote accepted

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()));
        }
});
share|improve this answer

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)));
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.