$npx -y skills add edutrul/drupal-ai --skill drupal-form-validationDrupal form validation — validateForm(), #element_validate, setError(), setErrorByName(), conditional validation (require fields based on other values), and custom validators.
| 1 | # Drupal Form Validation |
| 2 | |
| 3 | ## validateForm() Method |
| 4 | |
| 5 | ```php |
| 6 | public function validateForm(array &$form, FormStateInterface $form_state): void { |
| 7 | $name = $form_state->getValue('name'); |
| 8 | |
| 9 | if (strlen($name) < 3) { |
| 10 | $form_state->setErrorByName('name', $this->t('Name must be at least 3 characters.')); |
| 11 | } |
| 12 | |
| 13 | if (!preg_match('/^[a-zA-Z\s]+$/', $name)) { |
| 14 | $form_state->setErrorByName('name', $this->t('Name may only contain letters and spaces.')); |
| 15 | } |
| 16 | } |
| 17 | ``` |
| 18 | |
| 19 | ## setError vs setErrorByName |
| 20 | |
| 21 | ```php |
| 22 | // Target a specific form element by name |
| 23 | $form_state->setErrorByName('field_date', $this->t('Invalid date.')); |
| 24 | |
| 25 | // Target a nested element |
| 26 | $form_state->setErrorByName('field_date][0][value', $this->t('Invalid date.')); |
| 27 | |
| 28 | // Target a specific render element directly |
| 29 | $form_state->setError($form['field_date'], $this->t('Invalid date.')); |
| 30 | ``` |
| 31 | |
| 32 | ## #element_validate |
| 33 | |
| 34 | Attach a validator directly to an element: |
| 35 | |
| 36 | ```php |
| 37 | $form['email'] = [ |
| 38 | '#type' => 'email', |
| 39 | '#title' => $this->t('Email'), |
| 40 | '#element_validate' => [ |
| 41 | [static::class, 'validateUniqueEmail'], |
| 42 | ], |
| 43 | ]; |
| 44 | |
| 45 | public static function validateUniqueEmail(array &$element, FormStateInterface $form_state, array &$form): void { |
| 46 | $email = $element['#value']; |
| 47 | // Check uniqueness... |
| 48 | if ($emailExists) { |
| 49 | $form_state->setError($element, t('This email is already registered.')); |
| 50 | } |
| 51 | } |
| 52 | ``` |
| 53 | |
| 54 | ## Conditional Validation |
| 55 | |
| 56 | ```php |
| 57 | public function validateForm(array &$form, FormStateInterface $form_state): void { |
| 58 | $type = $form_state->getValue('type'); |
| 59 | |
| 60 | if ($type === 'external') { |
| 61 | $url = $form_state->getValue('url'); |
| 62 | if (empty($url)) { |
| 63 | $form_state->setErrorByName('url', $this->t('URL is required for external type.')); |
| 64 | } |
| 65 | elseif (!UrlHelper::isValid($url, TRUE)) { |
| 66 | $form_state->setErrorByName('url', $this->t('Please enter a valid URL.')); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | ``` |
| 71 | |
| 72 | ## Accessing Values in Validation |
| 73 | |
| 74 | ```php |
| 75 | // Get a single value |
| 76 | $value = $form_state->getValue('my_field'); |
| 77 | |
| 78 | // Get nested value |
| 79 | $value = $form_state->getValue(['field_date', 0, 'value']); |
| 80 | |
| 81 | // Get all values |
| 82 | $values = $form_state->getValues(); |
| 83 | |
| 84 | // Check if field has errors |
| 85 | if ($form_state->getError($form['my_field'])) { ... } |
| 86 | ``` |
| 87 | |
| 88 | ## Adding Validation via hook_form_alter |
| 89 | |
| 90 | ```php |
| 91 | #[Hook('form_node_article_form_alter')] |
| 92 | public function formNodeArticleFormAlter(array &$form, FormStateInterface $form_state): void { |
| 93 | // Add before existing validators |
| 94 | array_unshift($form['#validate'], [static::class, 'validateArticle']); |
| 95 | |
| 96 | // Add after existing validators |
| 97 | $form['#validate'][] = [static::class, 'validateArticle']; |
| 98 | } |
| 99 | |
| 100 | public static function validateArticle(array &$form, FormStateInterface $form_state): void { |
| 101 | // Validation logic here. |
| 102 | } |
| 103 | ``` |