简体   繁体   中英

Symfony, Hidden Form Field

i have problem with hiding my form fields. For example:

        ->add('new_password', 'repeated', array(

            'first_options' => array(
                'label' => 'Nowe hasło',
                'attr'  =>  array('style'=>'display:none;')),
            'second_options' => array(
                'label' => 'Powtórz nowe hasło',
                'attr'  =>  array('style'=>'display:none;')),
            'mapped' => false,
            'required' => false,
        ));

Field is not visible but label is visible. I want to have hidden field but label should be hidden to. I want to show it in Jquery after clicking on the button. Any ideas guys ?

First, this is not a Hidden Field Type but a repeated type you want to hide by passing a style='display:none;' attribute.

In general, if you don't want to display a given label, you may need to customize your form rendering.

For example,

{{ form_row(yourForm.new_password) }} {# in case you're using the form_row helper #}

should be replaced by

{{form_widget(yourForm.new_password) }}

Because form_row(yourForm.yourField) is in fact a shortcut for,

{{ form_errors(yourForm.yourField) }}
{{ form_label(yourForm.yourField) }}
{{ form_widget(yourForm.yourField) }}

Also,

Why do you need to hide a repeated password field this way?

If you want to show it only when you click on a button why dont you wrap the newPassword widget in a div with display none?

But if you want to add attributes to the label you can use the option label_attr like this:

{{ form_row(form.name, {'label_attr ':{'class':'hidden'}}) }}

or

->add('new_password', 'repeated', array(

            'first_options' => array(
                'label' => 'Nowe hasło',
                'label_attr'  =>  array('style'=>'display:none;')),
            'second_options' => array(
                'label' => 'Powtórz nowe hasło',
                'label_attr'  =>  array('style'=>'display:none;')),
            'mapped' => false,
            'required' => false,
        ));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM