Symfony2 FormBuilder表单标签不是使用所有表单

时间:2015-04-08 10:02:08

标签: html forms symfony twig

我在formbuilder中有一个表单

ColleagueType

     public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('firstname', 'text', array(
            'label' => 'Voornaam'
        ))
        ->add('lastname', 'text', array(
            'label' => 'Achternaam'
        ))
        ->add('email', 'email', array(
            'label' => 'Email'
        ))
        ->add('phonenumber', 'text', array(
            'label' => 'Telefoon Nummer'
        ))
        ->add('saveNewColleague', 'submit');
}

public function getName()
{
    return 'colleague';
}

我在我的树枝模板中使用了这样的表格:

newcolleague.html.twig

    <div class="container-fluid container-homepage">
    <div class="row">
        <div class="col-lg-6">
            {{ form_start(colleagueform) }}
                {{ form_widget(colleagueform.firstname) }}
                {{ form_widget(colleagueform.lastname) }}
        </div>
        <div class="col-lg-6">
                {{ form_widget(colleagueform.email) }}
                {{ form_widget(colleagueform.phonenumber) }}
        </div>
    </div>
    <div class="row">
        <div class="col-lg-12">
                {{ form_widget(colleagueform.saveNewColleague) }}
            {{ form_end(colleagueform) }}
        </div>
    </div>
</div>

但formbuilder仅使用<form>标记表示前两个表单

呈现表格

    <div class="container-fluid container-homepage">
    <div class="row">
        <div class="col-lg-6">
            <form name="colleague" method="post" action="">
                            <input id="colleague_firstname" name="colleague[firstname]" required="required" class="form-control" type="text">
                            <input id="colleague_lastname" name="colleague[lastname]" required="required" class="form-control" type="text">
        </form></div>
        <div class="col-lg-6">
                            <input id="colleague_email" name="colleague[email]" required="required" class="form-control" type="email">
                            <input id="colleague_phonenumber" name="colleague[phonenumber]" required="required" class="form-control" type="text">
        </div>
    </div>
    <div class="row">
        <div class="col-lg-12">
                <button type="submit" id="colleague_saveNewColleague" name="colleague[saveNewColleague]" class="btn">Save new colleague</button>
                        <input id="colleague__token" name="colleague[_token]" class="form-control" value="od_0C8daL7V4G0QQFikLHZUBNtE86TSAUIEqbjH9eHk" type="hidden">
        </div>
    </div>
</div>

我该如何解决这个问题? 感谢:)

1 个答案:

答案 0 :(得分:1)

您的模板没有正确的HTML结构。表单开始标记与表单结束标记不在同一个div中。正确的模板如下所示:

<div class="container-fluid container-homepage">
    {{ form_start(colleagueform) }}
    <div class="row">
        <div class="col-lg-6">
            {{ form_widget(colleagueform.firstname) }}
            {{ form_widget(colleagueform.lastname) }}
        </div>
        <div class="col-lg-6">
            {{ form_widget(colleagueform.email) }}
            {{ form_widget(colleagueform.phonenumber) }}
        </div>
    </div>
    <div class="row">
        <div class="col-lg-12">
            {{ form_widget(colleagueform.saveNewColleague) }}
        </div>
    </div>
    {{ form_end(colleagueform) }}
</div>