Sahin Urkin
Sahin Urkin

Reputation: 279

Set ButtonType id in FormType, not in twig file?

I have this buildForm method in FormType file:

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'
            )
        ));

}

Then i render form in twig file:

            <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>

But button does not have id with saveButton, instead:

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

When i set id in twig file, like this, it's work fine:

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

Upvotes: 1

Views: 272

Answers (1)

J. Almandos
J. Almandos

Reputation: 347

The ID of the button will be the first parameter of the add() function (in your case, "submit"). That's why you get "app_bundle_comment_form_type_submit" on your id. To delete the rest of the id, there's a function in the FormType file called getBlockPrefix(). Just set the return value to "" and you're done.

Upvotes: 2

Related Questions