5

I've created a module for Drupal8 that checks against values in the user register and set an error if the values do no match with something you have entered in a custom form.

My module has got a form where you add a list of username you don't want the user to take and a custom message to display as an error.

Forms works perfectly, I can store data, edit it etc... It is stored under src/Form/ and settings are under config/install

Now when I'm trying to hook the user register form validate I got an error regarding form_set_error():

PHP Fatal error: Call to undefined function form_set_error()

Also I think I'm not using $form_state in the right way (but it does the job)

Here's the code in my_module.module:

function my_module_form_user_register_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
 $form['#validate'][] = 'my_module_user_validate';
}

/**
 * Custom validation function.
 */
function my_module_user_validate(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
    // get module settings
    $config = \Drupal::config('my_module.settings');

    // Convert $form_state to an array as it has been passed as object, it looks wrong but I could not find any documentation at the moment
    $form_state = get_object_vars($form_state);
    $name = $form_state['values']['name']);
    $usernames = $config->get('my_modules_usernames');
    // Username are inserted in a text area separated by an enter key
    $usernames = $usernames != '' ? explode("\r\n", $usernames) : array();
    foreach ($usernames as $username) {
      if ($username == $name[1]) {
        //apparently in Drupal 8 form_set_error does not work anymmore
        form_set_error('mail', $config->get('my_module_message'));
      }
    }
  } 
5

The $form_state now become a classed object as documented here

You can use getValue() to get field value instead of converting it into an array

$form_state->getValue('name');

instead of:

$form_state = get_object_vars($form_state);
$name = $form_state['values']['name']);

Same thing for form_set_value.

And form_set_error() became $form_state->setErrorByName().

2

The form_set_error() has been dropped in favor of the following $form_state class methods:

$form_state->setError($element, $message);
$form_state->setErrorByName($element, $message);

So the code would look like:

use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_alter().
 */
function mymodule_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  // Use this to find out about the current form id.
  drupal_set_message('Form ID: ' . $form_id);
}

/**
 * Implements hook_form_FORM_ID_alter() for the FORM_ID() form.
 */
function mymodule_form_FORM_ID_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['#validate'][] = '_mymodule_form_validate';
}

/**
 * Validates submission values in the FORM_ID() form.
 */
function _mymodule_form_validate(array &$form, FormStateInterface $form_state) {
  $values = $form_state->getValue('field_myfield');
  foreach ($values as $value) {
    $plain_value = is_array($value) ? current($value) : NULL;
    if (empty($plain_value)) {
      $form_state->setErrorByName('field_myfield', t('Please enter a value!'));
    }
  }
}

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.