Установить идентификатор ButtonType в FormType, а не в файл ветки? - PullRequest
0 голосов
/ 24 октября 2018

У меня есть этот buildForm метод в FormType файле:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('content', TextareaType::class, array(
            'label' => 'Коментар',
            'attr' => array(
                'class' => 'form-control input_box'
            )
        ))
        ->add('submit', ButtonType::class, array(
            'label' => 'SEND',
            'attr' => array(
                'id' => 'saveButton'
            )
        ));

}

Затем я отрисовываю форму в файле ветки:

            <div class="post_comment">
                <h3>Add comment</h3>
                {{ form_start(commentForm) }}
                    <!--div class="col-md-6">
                        <h4>Name</h4>
                        <input type="text" class="form-control input_box" id="fullname" placeholder="">
                    </div>
                    <div class="col-md-6">
                        <h4>Email</h4>
                        <input type="text" class="form-control input_box" id="email" placeholder="">
                    </div-->
                    <div class="col-md-12">
                        <h4>{{ form_label(commentForm.content) }}</h4>
                        {{ form_widget(commentForm.content) }}
                        {{ form_widget(commentForm.submit) }}
                    </div>
                {{ form_end(commentForm) }}
            </div>

Но у кнопки нет идентификатора с saveButton, вместо:

<button type="button" id="app_bundle_comment_form_type_submit" name="app_bundle_comment_form_type[submit]">SEND</button>

Когда я устанавливаю идентификатор в файл ветки, вот так, все работает нормально:

{{ form_widget(commentForm.submit, {'id': 'saveButton' }) }}

1 Ответ

0 голосов
/ 24 октября 2018

Идентификатор кнопки будет первым параметром функции add () (в вашем случае, «submit»).Вот почему вы получаете "app_bundle_comment_form_type_ submit " на свой идентификатор.Чтобы удалить оставшуюся часть идентификатора, в файле FormType есть функция getBlockPrefix ().Просто установите возвращаемое значение на "", и все готово.

...