rtom
rtom

Reputation: 209

Don't render form field, symfony

I don't want some fields to show. I tried it like this

{{form_start(form)}}
{{form_widget(form)}}
{% do form.password.setRendered %}
{{ form_end(form) }}

But It doesn't work.

This is my form class. I don't want password field showing.

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
                ->add('username')
                ->add('email')
                ->add('password')
                ->add('type', ChoiceType::class, array(
                    'choices' => array(
                        'User' => 'ROLE_USER',
                        'Admin' => 'ROLE_ADMIN',
                        ),
                    ))
                ->add('save', SubmitType::class)
                ;
    }
}

Upvotes: 0

Views: 1547

Answers (2)

Michaël Perrin
Michaël Perrin

Reputation: 6238

You need to change the order of the lines and first tell that password field is rendered, before rendering the form itself:

{{ form_start(form) }}

{% do form.password.setRendered %}
{{ form_widget(form) }}

{{ form_end(form) }}

Upvotes: 4

Dan Costinel
Dan Costinel

Reputation: 1736

From the Form/UserType.php class, remove this line:

->add('password')

And it won't be rendered anymore.

Upvotes: -1

Related Questions