3

I'm working on a form extension that can disable some fields in form types when the underlying data is not new.I do this so I don't have to create seperate forms or event listeners for update/creation forms.i.e: I create the entity creation form and the extension disables the fields signified by an option if the underlying entity is not new.I bound an event listener to the PRE_SET_DATA event as such:

class RemoveFieldsExtension extends AbstractTypeExtension{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    if (!$options['disable_on_update']) {
        return;
    }

    $isNewCallback = $options['disable_on_update_is_new'];
    $em = $this->em;
    $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($em, $isNewCallback, $builder){
            if(is_bool($isNewCallback))
                return $isNewCallback;

            if(is_callable($isNewCallback)){
                $isNew = call_user_func($isNewCallback, $event->getData(), $em);
                if(!$isNew){//the check for resolving if this is an update form or creation form
                 //   $builder->setDisabled(true);
                    $form = $event->getForm();
                    $form->getConfig()->setDisabled(true);
                }
            }
        },
        -200
    );

}
}

the extension works fine the problem is that the above code will produce 'FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.'
it also didn't work when I tried $builder->setDisabled(true);.what can I do to accomplish this?

user2268997
  • 1,263
  • 2
  • 14
  • 35

0 Answers0