You are here

public function TablefieldWidget::validateTablefield in TableField 8.2

Validation handler.

File

src/Plugin/Field/FieldWidget/TablefieldWidget.php, line 188

Class

TablefieldWidget
Plugin implementation of the 'tablefield' widget.

Namespace

Drupal\tablefield\Plugin\Field\FieldWidget

Code

public function validateTablefield(array &$element, FormStateInterface &$form_state, array $form) {
  if ($element['#required'] && $form_state
    ->getTriggeringElement()['#type'] == 'submit') {
    $items = new FieldItemList($this->fieldDefinition);
    $this
      ->extractFormValues($items, $form, $form_state);
    $values = FALSE;
    if (isset($element['#value'])) {
      foreach ($element['#value']['tablefield']['table'] as $row) {
        foreach ($row as $cell) {
          if (empty($cell)) {
            $values = TRUE;
            break;
          }
        }
      }
    }
    if (!$items
      ->count() && $values == TRUE) {
      $form_state
        ->setError($element, $this
        ->t('@name field is required.', [
        '@name' => $this->fieldDefinition
          ->getLabel(),
      ]));
    }
  }
}