You are here

public static function GeofieldMapFormElementsValidationTrait::jsonValidate in Geofield Map 8.2

Form element json format validation handler.

File

src/GeofieldMapFormElementsValidationTrait.php, line 82

Class

GeofieldMapFormElementsValidationTrait
Class GeofieldMapFormElementsValidationTrait.

Namespace

Drupal\geofield_map

Code

public static function jsonValidate($element, FormStateInterface &$form_state) {
  $element_values_array = Json::decode($element['#value']);

  // Check the jsonValue.
  if (!empty($element['#value']) && $element_values_array == NULL) {
    $form_state
      ->setError($element, t('The @field field is not valid Json Format.', [
      '@field' => $element['#title'],
    ]));
  }
  elseif (!empty($element['#value'])) {
    $form_state
      ->setValueForElement($element, Json::encode($element_values_array));
  }
}