Skip to content
Snippets Groups Projects
Commit 643f1365 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2076321 by yched, Mac_Weber, pcambra: Link "title" validation should use a static method

parent c0161655
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -133,6 +133,8 @@ protected static function getUserEnteredStringAsUri($string) {
}
/**
* Form element validation handler for the 'uri' element.
*
* Disallows saving inaccessible or untrusted URLs.
*/
public static function validateUriElement($element, FormStateInterface $form_state, $form) {
......@@ -149,6 +151,18 @@ public static function validateUriElement($element, FormStateInterface $form_sta
}
}
/**
* Form element validation handler for the 'title' element.
*
* Conditionally requires the link title if a URL value was filled in.
*/
public static function validateTitleElement(&$element, FormStateInterface $form_state, $form) {
if ($element['uri']['#value'] !== '' && $element['title']['#value'] === '') {
$element['title']['#required'] = TRUE;
$form_state->setError($element['title'], t('!name field is required.', array('!name' => $element['title']['#title'])));
}
}
/**
* {@inheritdoc}
*/
......@@ -207,7 +221,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
// non-empty. Omit the validation on the field edit form, since the field
// settings cannot be saved otherwise.
if (!$this->isDefaultValueWidget($form_state) && $this->getFieldSetting('title') == DRUPAL_REQUIRED) {
$element['#element_validate'][] = array($this, 'validateTitle');
$element['#element_validate'][] = array(get_called_class(), 'validateTitleElement');
}
// Exposing the attributes array in the widget is left for alternate and more
......@@ -311,18 +325,6 @@ public function settingsSummary() {
return $summary;
}
/**
* Form element validation handler; Validates the title property.
*
* Conditionally requires the link title if a URL value was filled in.
*/
public function validateTitle(&$element, FormStateInterface $form_state, $form) {
if ($element['uri']['#value'] !== '' && $element['title']['#value'] === '') {
$element['title']['#required'] = TRUE;
$form_state->setError($element['title'], $this->t('!name field is required.', array('!name' => $element['title']['#title'])));
}
}
/**
* {@inheritdoc}
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment