You are here

public static function KeyValueWidgetTrait::validateKeyElement in Key value field 8

Form element validation handler for the 'key' element.

Conditionally requires the key if a value is filled in.

File

src/Plugin/Field/FieldWidget/KeyValueWidgetTrait.php, line 261

Class

KeyValueWidgetTrait
Common traits for key value field widgets inheriting from different widgets.

Namespace

Drupal\key_value_field\Plugin\Field\FieldWidget

Code

public static function validateKeyElement(&$element, FormStateInterface $form_state, $form) {
  if (!empty($element['value']['#value']) && empty($element['key']['#value'])) {
    $element['key']['#required'] = TRUE;
    $form_state
      ->setError($element['key'], t('@name field is required.', [
      '@name' => $element['key']['#title'],
    ]));
  }
}