1
votes

I'm trying to put two hidden fields in a form for a particular user. I have this code:

<div id="mio_miobundle_empleadotype">
{{ form_row(edit_form.dni) }}
{{ form_row(edit_form.nombre) }}
{{ form_row(edit_form.username) }}
{{ form_row(edit_form.apellido1) }}
{{ form_row(edit_form.apellido2) }}
{{ form_row(edit_form.localidad) }}
{{ form_row(edit_form.provincia) }}
{{ form_row(edit_form.telefono) }}
{{ form_row(edit_form.movil) }}
{{ form_row(edit_form.email) }}
{{ form_row(edit_form.direccion) }}
{% if is_granted('ROLE_A') %}     
      {{ form_row(edit_form.activo) }}
      {{ form_row(edit_form.role) }}
{%endif%}
{{ form_row(edit_form.password) }}

and:

$builder
            ->add('dni','text',array('label' => 'Dni'))
            ->add('nombre','text',array('label' => 'Nombre'))
            ->add('username','text',array('label' => 'Usuario'))
            ->add('apellido1','text',array('label' => 'Apellido1'))
            ->add('apellido2','text',array('label' => 'Apellido2'))
            ->add('email','email',array('label' => 'Email'))
            ->add('localidad','text',array('label' => 'Localidad'))
            ->add('provincia','text',array('label' => 'Provincia'))
            ->add('telefono','text',array('label' => 'Teléfono'))
            ->add('movil','text',array('label' => 'Móvil'))
            ->add('direccion','text',array('label' => 'Dirección'))
            ->add('activo')
            ->add('role')
            ->add('password', 'repeated', array('first_name' => 'Nueva contraseña','second_name' => 'Repite contraseña','type' => 'password' ,'invalid_message'=> 'Las contraseñas deben ser iguales.'))
        ;
    }

but I get this error:

Catchable Fatal Error: Argument 1 passed to mio\mioBundle\Entity\Empleado::setRole() must be an instance of mio\mioBundle\Entity\Role, null given, called in /var/www/Symfony/vendor/symfony/src/Symfony/Component/Form/Util/PropertyPath.php on line 347 and defined in /var/www/Symfony/src/mio/mioBundle/Entity/Empleado.php line 289

I say that because I have to fill in the state are any help?

2
Why do you need those hidden fields?Elnur Abdurrakhimov

2 Answers

0
votes

Place the checking role code in the controller

public function someAction()
{
    $form = $this->createFrom( // ...
    if (false === $this->get('security.context')->isGranted('ROLE_A')) {
        $form->remove('activo');
        $form->remove('role');
    }

    // ...
}

And in the template check if form field is defined

{{ edit_form.activo is defined ? form_row(edit_form.activo) : '' }}
{{ edit_form.role is defined ? form_row(edit_form.role) : '' }}
-1
votes

Instead of not rendering the fields completely, try hiding the fields from view using css.

{% if is_granted('ROLE_A') %}     
      {{ form_row(edit_form.activo) }}
      {{ form_row(edit_form.role) }}
{% else %}
      {{ form_widget(edit_form.activo, { 'attr': {'class': 'hide'} }) }}
      {{ form_widget(edit_form.role, { 'attr': {'class': 'hide'} }) }}
{% endif %}