0

I have a form that extends the native RegisterForm

When this form is submitted, I would like to register a user, but then I would also like to store the uuid & certain fields like name & email in my external database.

I have got the form working in terms of registering a user, but the moment I add a submitForm() function (to get the uuid, name & email to store in my external database), I get a error

Please any help & guidance would be greatly appreciated.

My code looks as follows: NewUserRegisterForm.php

    <?php

namespace Drupal\business_listing\Form;

use Drupal\Core\Form\FormStateInterface;
use Drupal\user\RegisterForm;

/**
 * Provides a user login form.
 */
class NewUserRegisterForm extends RegisterForm {

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

        $form = parent::form($form, $form_state);

        // Change the form here
        $form['test'] = [
          '#markup' => '<p>Test extended form</p>',
        ];
        return $form;
    }

// uncommenting submit results in error
//  public function submitForm(array &$form, FormStateInterface $form_state) {

//      get the created users uuid & perform additional database queries

//  }

}

.module

    /**
 * Implements hook_entity_build
 */
function business_listing_entity_type_build(array &$entity_types) {
    $entity_types['user']->setFormClass('business_listing_register', 'Drupal\business_listing\Form\NewUserRegisterForm');
}

RegistrationController.php

public function register() {
    $build = [];

    $entity = \Drupal::entityManager()
        ->getStorage('user')
        ->create(array());

    $formObject = \Drupal::entityManager()
        ->getFormObject('user', 'business_listing_register')
        ->setEntity($entity);

    $build['form'] = \Drupal::formBuilder()->getForm($formObject);
    return $build;
}

The error i get when I uncomment submitForm()

The website encountered an unexpected error. Please try again later.Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null: INSERT INTO {users_field_data} (uid, langcode, preferred_langcode, preferred_admin_langcode, name, pass, mail, timezone, status, created, changed, access, login, init, default_langcode) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9, :db_insert_placeholder_10, :db_insert_placeholder_11, :db_insert_placeholder_12, :db_insert_placeholder_13, :db_insert_placeholder_14); Array ( [:db_insert_placeholder_0] => 36 [:db_insert_placeholder_1] => en [:db_insert_placeholder_2] => en [:db_insert_placeholder_3] => [:db_insert_placeholder_4] => [:db_insert_placeholder_5] => [:db_insert_placeholder_6] => [:db_insert_placeholder_7] => Europe/Berlin [:db_insert_placeholder_8] => 0 [:db_insert_placeholder_9] => 1556273327 [:db_insert_placeholder_10] => 1556273327 [:db_insert_placeholder_11] => 0 [:db_insert_placeholder_12] => 0 [:db_insert_placeholder_13] => [:db_insert_placeholder_14] => 1 )

Thanks in advance :)

Also, I was not sure what I should take this post as forms tag says "Don't use it for entity or entity field forms."

  • 1
    What is in submitForm()? It should be at least parent::submitForm($form, $form_state); – 4k4 Apr 26 at 12:05

Your Answer

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

Browse other questions tagged or ask your own question.