简体   繁体   中英

How to set attributes for form field rendered by two inputs in Symfony2

I have date field in symfony2 which is rendered by two inputs:

   $builder

            ->add('timeStart','datetime',array(
                'date_widget' => 'single_text',
                'time_widget' => 'single_text',
                'date_format' => 'yyyy-MM-dd',
                'data' => new \DateTime('now')
            ))

how to set attributes for each input? When I add attr=>array('some_attr'=>'some_value') it is added to the div in which those inputs are, not to each input.

You need to set each's input attribute when rendering inside the twig template. Something like this would do:

{{ form_widget(form.timeStart, { attributes1 } ) }}

And

{{ form_widget(form.timeStart, { attributes2 } ) }}

Take a look at the docs .

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