// Get the value of the text field $text_field_value = $form_state->getValue('text_field'); // Check if the field is empty if (empty($text_field_value)) { // Set error message for the text field $form_state->setErrorByName('text_field', t('Text field should not be empty.')); }
// Get the value of the select field $select_field_value = $form_state->getValue('select_field'); // Check if no value is selected if (empty($select_field_value)) { // Set error message for the select field $form_state->setErrorByName('select_field', t('Please select a value from the dropdown.')); }Package/Library: Drupal Core.