/** * Implements hook_form_alter(). */ function mymodule_form_alter(&$form, &$form_state, $form_id) { if ($form_id == 'user_register_form') { // Set default value for username. $form['account']['name']['#default_value'] = 'john_doe'; // Set user input for username. $form_state->setUserInput(['account' => ['name' => 'john_doe']]); } }
/** * Implements hook_validate(). */ function mymodule_validate($form, &$form_state) { // Get user input for email. $email = $form_state->getUserInput()['email']; // Validate email. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $form_state->setErrorByName('email', 'Invalid email address.'); } }In this example, we are retrieving the user input for the email field using the getUserInput method. We are then validating the email address using the filter_var function. If the email is invalid, we are using the setErrorByName method to set an error for the email field.