Symfony, do not show a field in a Type form

0

Good, I have a form in Symfony of type "type" that shows a series of attributes, but in a specific case I would like to show the whole form except one field.

I tried to pass from the controller to the constructor of the buildForm in the Options parameter a parameter to then collect and do the desired action but it does not work for me.

The code I've tried is this

class FormativeActionType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title',TextType::class, array(
                'label' => 'common.title'
            ))
            ->add('officialCode',TextType::class, array(
                'label' => 'common.officialCode',
                'disabledCar' => true,
            ))
            ->add('enabled',CheckboxType::class, array(
                'required' => false  ,
                'label' => 'common.enabled'
            ));
    } 

}

And from the controller I tried this

$car = new Car();

$form = $this->createForm(CarType::class, $coche, [
    'action' => $this->generateUrl('admin_car_new'),
    'disabledCar' => true
]);

$form->handleRequest($request);
    
asked by ilernet 02.02.2018 в 16:10
source

1 answer

1

try your controler to pass the information of the action you want to do in an array

$form = $this->createForm('BackendBundle\Form\CarType', $coche, array(
        'accion' => $accion, //mandamos la informacion para que nos enseñe o no alguna de las opciones del formulario
    ));

and then remember to define it in the configuration of your CarType

public function configureOptions(OptionsResolver $resolver) {
    $resolver->setDefaults(array(
        'data_class' => 'BackendBundle\Entity\CarAcciones',
        'accion' => 'ADMIN',
    ));
 }

in this case by default it is ADMIN

and then you can take it out for what you need, for example with an if

if ('USER' === $options['accion']) {
//lo que tenga que pasar

 }
    
answered by 03.02.2018 / 13:21
source