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