Home > Backend Development > PHP Tutorial > symfony3创建表单后怎么给表单添加class

symfony3创建表单后怎么给表单添加class

WBOY
Release: 2016-06-06 20:17:40
Original
1438 people have browsed it

symfony自带的创建表单功能,创建表单后,在模板直接这样就可以显示:

{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}

请问能不能再创建表单的时候,为要创建的表单元素都添加一个class,这样给前端,前端更好写样式,以下是创建表单代码:

<code>$task = new Task();
$task->setTask('测试表单');
$task->setDueDate(new \DateTime('tomorrow'));

$form = $this->createFormBuilder($task)->add('task', TextType::class)
    ->add('dueDate', DateType::class)
    ->add('save', SubmitType::class, array('label' => 'Create task'))
    ->getForm();</code>
Copy after login
Copy after login

回复内容:

symfony自带的创建表单功能,创建表单后,在模板直接这样就可以显示:

{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}

请问能不能再创建表单的时候,为要创建的表单元素都添加一个class,这样给前端,前端更好写样式,以下是创建表单代码:

<code>$task = new Task();
$task->setTask('测试表单');
$task->setDueDate(new \DateTime('tomorrow'));

$form = $this->createFormBuilder($task)->add('task', TextType::class)
    ->add('dueDate', DateType::class)
    ->add('save', SubmitType::class, array('label' => 'Create task'))
    ->getForm();</code>
Copy after login
Copy after login

Symfony的form在Twig模板里可以每个表单组件单独渲染,同时设置各自的class(或者其他)属性,如下:

<code>{{ form_start(form) }}
{{ form_widget(form.field1, { 'attr': { 'class': 'class1' } }) }}
{{ form_widget(form.field2, { 'attr': { 'class': 'class2' } }) }}
{{ form_widget(form.field3, { 'attr': { 'class': 'class3' } }) }}
{{ form_widget(form.field4, { 'attr': { 'class': 'class4' } }) }}
{{ form_end(form) }}</code>
Copy after login

$form = $this->createForm(new AnswerType(), new Answer(), [

<code>        'action' => $this->generateUrl('eb5_qas_reply', ['id' => $id]),
        'method' => 'POST',
        'attr' => ['class' => 'form-horizontal']
    ]);
    
   创建form attr属性</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template