Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add model transformer for a certain form field in form events

Tags:

forms

symfony

The only way that works I have found is to add model transformer in buildForm method of a form type, as these codes below:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $transformer = new IssueToNumberTransformer($entityManager);
    $builder->add(
        $builder->create('issue', 'text')->addModelTransformer($transformer)
    ); 
}

But I have a form field which displays when a another form field has a valid value, so I'd rather create the form field in FormEvent::PRE_SET_DATA event.

1. one wrong way

->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($builder) {
     /** @var $order \VMSP\OrderBundle\Entity\OrderInterface */
     $order = $event->getData();
     $form  = $event->getForm();
     /** @var $serviceType \VMSP\StoreBundle\Entity\ServiceType */
     $serviceType = $order->getServiceType();

     //only home service needs user's address
     if ($serviceType && $serviceType->getType() == ServiceType::TYPE_HOME_SERVICE) {
         //won't work
         $form->add(
             $builder->create('address','hidden')
                     ->addModelTransformer($this->addressTransformer),
             array(
                 'label' => 'vmsp_order.contact.form.address',
             )
         );
    }
}

somebody suggested

$form->add(
   $builder->create('address', 'hidden')
           ->addModelTransformer($this->addressTransformer),
   array( 'label' => 'vmsp_order.contact.form.address')
);

unfortunately, it throws this error:

Expected argument of type "string, integer or Symfony\Component\Form\FormInterface", "Symfony\Component\Form\FormBuilder" given

2. another wrong way

if ($serviceType && $serviceType->getType() == ServiceType::TYPE_HOME_SERVICE) {
    $form->add(
        'address', 
        'hidden', 
        array('label' => 'vmsp_order.contact.form.address')
    );

    $form->get('address')
         ->getConfig()
         ->addModelTransformer($this->addressTransformer);
}

got error:

FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.

I list the two wrong ways above, because I find lots of post saying these ways are right, of course, they are not. this post is a same question like symfony2-form-events-and-model-transformers , but that answer is not what I need, so my question is, any way to add model transformer in form events for a certain form field?

like image 274
vidy videni Avatar asked Nov 10 '22 07:11

vidy videni


1 Answers

Your "1. one wrong way" doesn't work because $builder->addModelTransformer() returns a FormConfigBuilderInterface but $form->add() expects a FormInterface (see your error message).

To make it work, just add getForm() :

$form->add(
    $builder->create('address','hidden')
        ->addModelTransformer($this->addressTransformer)
        ->getForm()   // Creates the form
        ...
like image 66
gblock Avatar answered Nov 15 '22 11:11

gblock