1

I have a form with a <button> element

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['address'] = array(
      '#type' => 'textfield',
      '#autocomplete_route_name' => 'geocoder_autocomplete.autocomplete',
      '#title' => '<span>' . t('Find an adoration place') . '</span>',
    );

    $form['submit'] = [
      '#type' => 'inline_template',
      '#template' => '<button type="submit" class="glyphicon glyphicon-search"></button>',
    ];

    return $form;
  }

When I click the button the page is refreshed but the submitForm() method is not called. If I add an <input type='button'> element in the form and I click the <button> or the <input type='button'> the submitForm() is called.

  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['address'] = array(
      '#type' => 'textfield',
      '#autocomplete_route_name' => 'geocoder_autocomplete.autocomplete',
      '#title' => '<span>' . t('Find an adoration place') . '</span>',
    );

    $form['submit'] = [
      '#type' => 'inline_template',
      '#template' => '<button type="submit" class="glyphicon glyphicon-search"></button>',
    ];

    $form['submit1'] = [
      '#type' => 'submit',
      '#value' => $this->t('Submit'),
    ];

    return $form;
  }

I'm missing something here? Why my button element don't trigger the submitForm method?

4

Your first "submit" button doesn't get recognized by Drupal as a button because you use an inline template.

If you want to add a class on your button use:

$form['submit'] = [
  '#type' => 'submit',
  '#value' => $this->t('Submit'),
  '#attributes'=> ['class'=>['glyphicon', 'glyphicon-search']],
];
| improve this answer | |
  • thanks for your answer, this is the answer for my question, but I really need a <button> element in my form because the client want a icon in the button and with the <input type='button'> you can't do it, any ideas? – Adrian Cid Almaguer Mar 28 '17 at 16:09
  • Ofcourse you can. You can use the :after or :before selector in CSS... – Robin Mar 28 '17 at 16:10
  • Or set a background on the submit button via CSS – Robin Mar 28 '17 at 16:10
  • I'm not very skilled with css, can you share an example, please? – Adrian Cid Almaguer Mar 28 '17 at 16:12
  • lmgtfy.com/?q=css3+%3Aafter+icon – Robin Mar 28 '17 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.