Search code examples
phpsymfonysymfony-2.1

How to add another field value to entry_options


I have 1 childtype which should get type fields value.

 $builder->add('rows', TableCollectionType::class, array(
     'entry_type' => HomeType::class,
     'entry_options' => ['test' => $test]
 ))
 ->add('type', HiddenType::class, [
     'constraints' => [
         new NotBlank(),
         new Choice(TypeClassifier::VALID_TYPES)
      ]
 ]);

tried to add eventlistener but unsuccessfully:

$form->get('rows')->add('edit_type', HiddenType::class, ['data' => $data['type']]);

Is there any possibility to pass type value to child type?


Solution

  • I wanted to validate using that parameter. So fixed problem adding callback to parent type:

     $builder->add('rows', TableCollectionType::class, array(
        'entry_type' => HomeType::class,
        'constraints' => [new Callback([$this, 'validateTest'])]
     ))
    

    then in methode validateTest you can get type:

    public function validateCosts($rows, ExecutionContext $context){
         $form = $context->getRoot();
         $data = $form->getData();
         $type = $data['type];
    }