0

I have a module that creates a form in a block with a template. But when I press the submit button, it does not execute the submitForm function. Instead if in the template only {{form}} if you do.

To clarify, in the template the fields are customized and I call them: {{form.date}}, {{form.submit}}

/**
 * {@inheritdoc}
 */
public function getFormId() {
    return 'custom_form';
}

/**
 * {@inheritdoc}
 */
public function buildForm(array $form, FormStateInterface $form_state) {

    $form['name'] = [
        '#type' => 'textfield',
        '#size' => 60,
        '#placeholder' => 'Votre nom',
        '#maxlength' => 128,
        '#required' => TRUE,
    ];



    $form['tel'] = [
        '#type' => 'number',
        '#default_value' => "",
        '#placeholder' => 'Votre numéro',
        '#size' => 20,
        '#maxlength' => 20,
    ];




    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => $this->t('Réserver'),
        '#button_type' => 'primary',
    );



    return $form;
}


 /**
 * {@inheritdoc}
 */
 public function submitForm(array &$form, FormStateInterface $form_state) {

foreach ($form_state->getValues() as $key => $value) {
  drupal_set_message($key . ': ' . $value);
}
 }
4
  • We need to see your code to ba able to help you.
    – sanzante
    Mar 22, 2017 at 11:58
  • Update the question
    – Fen Dev
    Mar 22, 2017 at 12:03
  • How do you know submit is not executed? because you don't see the drupal set message? Sometimes is not realiable, can test in antoher way? using devel dd() function, for example.
    – sanzante
    Mar 22, 2017 at 12:21
  • Yes, please use debugging methods and not set message.
    – Kevin
    Mar 22, 2017 at 13:15

3 Answers 3

1

You have to write the submit handler for the form to save data.

  /**
   * {@inheritDoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // Insert queries.

    $query = \Drupal::database()->insert('mymodule_table');
    $query->fields([
      'name' => $form_state->getValue('name'),
      'tel' => $form_state->getValue('tel'),
    ]);
    $query->execute();

  }
1
  • You also have to render all of the form elements, not just specific pieces of it.
    – Kevin
    Mar 22, 2017 at 15:46
1

I believe you do have to render with {{ form }} because it will render more than just your two form items. This is why the submit is not executing. If that is all you have in your twig, then it would sound like you are missing a form tag and hidden form elements.

0

If you don't render with {{ form }}, you have to make sure to add these elements to your twig:

{{ element.form_build_id }} {# required #}
{{ element.form_id }} {# required #}

as well as adding the html manually and adding the appropriate attributes. This gives you more flexility in your template. But there are many advantages to just using {{ form }}, such as managing the elements entirely in your form class, rather than having to update two places. But sometimes if your layout is really odd, it may make more sense to break the form elements up in the template.

2
  • My problem now is that <form {{attributes}}> does not paint the attributes of the form, it's empty.
    – Fen Dev
    Mar 23, 2017 at 7:59
  • I already have those fields in place, and it stays the same. Does anybody have or know any example module in drupal 8?
    – Fen Dev
    Mar 23, 2017 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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