How Can I get autofocus on the first element (buil

2019-06-20 07:48发布

问题:

In my TopicType class, I used :

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
            ->add('title', 'text')
            ->add('content', 'ckeditor', array(
                'label' => 'Contenu',
                'config_name' => 'my_custom_config',
                'config' => array('language' => 'fr'),))
            ->add('save', 'submit')
    ;
}

How can I get autofocus on my first field "title", when i display the form?

回答1:

$builder->add('title', 'text', array(
    'attr' => array(
        'autofocus' => true
        )
    );


回答2:

The realy crossbrowser way is to type

$builder->add('title', 'text', array(
'attr' => array(
    'autofocus' => null
    )
);

This code produces just autofocus attribute without = sign and any value.



标签: forms symfony