mcpField
D
drupal-form-validation

drupal-form-validation

github.com/edutrulDevelopment7349 installsUpdated 21w ago
Repository

About drupal-form-validation

drupal-form-validation is a Drupal development skill for validating form input. It covers form-level and element-level validation, conditional rules, accessing values, and adding validators through form alteration.

Key features

  1. 01Validate values in validateForm()
  2. 02Attach errors to named or nested elements
  3. 03Add element-specific validators
  4. 04Apply conditional validation rules
  5. 05Add validators with hook_form_alter

Use cases

  1. 01Require a URL for a selected form type
  2. 02Validate a field's format or length
  3. 03Attach an error to a nested element
  4. 04Add custom checks to an existing form

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/edutrul/drupal-ai --skill drupal-form-validation

After installing, type/drupal-form-validationin Claude Code to confirm it loaded.

Bundled filesREADME.md

Details

From the project README

Drupal Form Validation

validateForm() Method

public function validateForm(array &$form, FormStateInterface $form_state): void {
  $name = $form_state->getValue('name');

  if (strlen($name) < 3) {
    $form_state->setErrorByName('name', $this->t('Name must be at least 3 characters.'));
  }

  if (!preg_match('/^[a-zA-Z\s]+$/', $name)) {
    $form_state->setErrorByName('name', $this->t('Name may only contain letters and spaces.'));
  }
}

setError vs setErrorByName

// Target a specific form element by name
$form_state->setErrorByName('field_date', $this->t('Invalid date.'));

// Target a nested element
$form_state->setErrorByName('field_date][0][value', $this->t('Invalid date.'));

// Target a specific render element directly
$form_state->setError($form['field_date'], $this->t('Invalid date.'));

#element_validate

Attach a validator directly to an element:

$form['email'] = [
  '#type' => 'email',
  '#title' => $this->t('Email'),
  '#element_validate' => [
    [static::class, 'validateUniqueEmail'],
  ],
];

public static function validateUniqueEmail(array &$element, FormStateInterface $form_state, array &$form): void {
  $email = $element['#value'];
  // Check uniqueness...
  if ($emailExists) {
    $form_state->setError($element, t('This email is already registered.'));
  }
}

Conditional Validation

public function validateForm(array &$form, FormStateInterface $form_state): void {
  $type = $form_state->getValue('type');

  if ($type === 'external') {
    $url = $form_state->getValue('url');
    if (empty($url)) {
      $form_state->setErrorByName('url', $this->t('URL is required for external type.'));
    }
    elseif (!UrlHelper::isValid($url, TRUE)) {
      $form_state->setErrorByName('url', $this->t('Please enter a valid URL.'));
    }
  }
}

Accessing Values in Validation

// Get a single value
$value = $form_state->getValue('my_field');

// Get nested value
$value = $form_state->getValue(['field_date', 0, 'value']);

// Get all values
$values = $form_state->getValues();

// Check if field has errors
if ($form_state->getError($form['my_field'])) { ... }

Adding Validation via hook_form_alter

#[Hook('form_node_article_form_alter')]
public function formNodeArticleFormAlter(array &$form, FormStateInterface $form_state): void {
  // Add before existing validators
  array_unshift($form['#validate'], [static::class, 'validateArticle']);

  // Add after existing validators
  $form['#validate'][] = [static::class, 'validateArticle'];
}

public static function validateArticle(array &$form, FormStateInterface $form_state): void {
  // Validation logic here.
}

Comments

Markdown supported