AgentStack
SKILL verified MIT Self-run

Drupal Form Validation

skill-edutrul-drupal-ai-drupal-form-validation · by edutrul

Drupal form validation — validateForm(), #element_validate, setError(), setErrorByName(), conditional validation (require fields based on other values), and custom validators.

No reviews yet
0 installs
11 views
0.0% view→install

Install

$ agentstack add skill-edutrul-drupal-ai-drupal-form-validation

✓ scanned · ✓ verified — works with Claude Code, Cursor, and more.

Security review

✓ Passed

No issues found. Passed automated security review. · v0.1.0 How review works →

  • Prompt-injection patterns
  • Secret / credential exfiltration
  • Dangerous shell & filesystem operations
  • Untrusted network calls
  • Known-malicious package signatures

What it can access

  • Network access No
  • Filesystem access No
  • Shell / process execution No
  • Environment & secrets No
  • Dynamic code execution No

From automated source analysis of v0.1.0. “Used” means the capability is present in the source — more access means more to trust, not that it’s unsafe.

Are you the author of Drupal Form Validation? Claim this listing to set pricing, connect Stripe payouts, and keep 70% of every sale.
Sign up to claim

About

Drupal Form Validation

validateForm() Method

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

  if (strlen($name) 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 hookformalter

#[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.
}

Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

Install and usage instructions live in the source repository linked above.

Reviews

No reviews yet — be the first.

Versions

  • v0.1.0 Imported from the upstream source.