aheadofall
aheadofall

Reputation: 1

Drupal submit contact form from api

Help, please, there is a task from the front on api to fill out the drupal contact form.

I have a contact form and a method, I need help with the code that will submit the data, I found this solution:

$form_state = new FormState();
$values = [
            'message' => 'test',
            'name'  => 'test'
          ];
$form_state->setValues($values);
\Drupal::formBuilder()->submitForm('data_portal_contact_us', $form_state);

In the log it gives the following error:

InvalidArgumentException: The form argument data_portal_contact_us is not a valid form. in Drupal\Core\Form\FormBuilder->getFormId() (line 197 of /www/core/lib/Drupal/Core/Form/FormBuilder.php).

Upvotes: 0

Views: 216

Answers (1)

Nik Ral
Nik Ral

Reputation: 122

Try to use The Form class Name in the $form_arg parameter of submitForm($form_arg, FormStateInterface &$form_state)

$values = [
            'message' => 'test',
            'name'  => 'test'
          ];
$form_state = (new FormState())->setValues($values);
\Drupal::formBuilder()->submitForm('Drupal\module_name\Form\customModuleNameForm', $form_state);

Upvotes: 0

Related Questions