一键导入
drupal-form-validation
Drupal form validation — validateForm(),
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Drupal form validation — validateForm(),
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
HTMX in Drupal 11.3+ core — the Htmx PHP fluent builder, dynamic forms with swapOob, partial routes with _htmx_route, response headers, and Drupal.behaviors integration. Use when building interactive UI without full-page reloads using Drupal's native HTMX support.
DDEV local development expertise. Use when working with DDEV projects, containers, configuration, or troubleshooting DDEV environments.
Custom Docker Compose local development patterns. Use when working with Docker-based local environments, container configuration, or troubleshooting Docker setups.
Drupal access control — permissions.yml, access callbacks, AccessResult, custom access checkers, and entity access.
Drupal Cache API — cache tags, contexts, max-age, cache bins, invalidation, and adding cache metadata to render arrays.
Drupal Composer management — requiring modules, updates, patches via composer-patches, and version constraints.
| name | drupal-form-validation |
| description | Drupal form validation — validateForm(), |
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.'));
}
}
// 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.'));
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.'));
}
}
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.'));
}
}
}
// 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'])) { ... }
#[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.
}