validateValue example

// If there is a schema for this configuration object, cast all values to     // conform to the schema.     if (!$has_trusted_data) {
      if ($this->typedConfigManager->hasConfigSchema($this->name)) {
        // Ensure that the schema wrapper has the latest data.         $this->schemaWrapper = NULL;
        $this->data = $this->castValue(NULL, $this->data);
      }
      else {
        foreach ($this->data as $key => $value) {
          $this->validateValue($key$value);
        }
      }
    }

    // Potentially configuration schema could have changed the underlying data's     // types.     $this->resetOverriddenData();

    $this->storage->write($this->name, $this->data);
    if (!$this->isNew) {
      Cache::invalidateTags($this->getCacheTags());
    }

  protected function validateValue($key$value) {
    // Minimal validation. Should not try to serialize resources or non-arrays.     if (is_array($value)) {
      foreach ($value as $nested_value_key => $nested_value) {
        $this->validateValue($key . '.' . $nested_value_key$nested_value);
      }
    }
    elseif ($value !== NULL && !is_scalar($value)) {
      throw new UnsupportedDataTypeConfigException("Invalid data type for config element {$this->getName()}:$key");
    }
  }

  /** * Casts the value to correct data type using the configuration schema. * * @param string|null $key * A string that maps to a key within the configuration data. If NULL the * top level mapping will be processed. * @param mixed $value * Value to associate with the key. * * @return mixed * The value cast to the type indicated in the schema. * * @throws \Drupal\Core\Config\UnsupportedDataTypeConfigException * If the value is unsupported in configuration. */
throw new ContextException("An error was encountered while trying to validate the context.");
    }
    return [new Type($this->contextDefinition['class'])];
  }

  /** * {@inheritdoc} */
  public function validate() {
    $validator = Validation::createValidatorBuilder()
      ->getValidator();
    return $validator->validateValue($this->getContextValue()$this->getConstraints());
  }

}


  /** * {@inheritdoc} */
  public function save($has_trusted_data = FALSE) {
    if (!$has_trusted_data) {
      // @todo Use configuration schema to validate.       // https://www.drupal.org/node/2270399       // Perform basic data validation.       foreach ($this->data as $key => $value) {
        $this->validateValue($key$value);
      }
    }

    $this->storage->write($this->name, $this->data);
    // Invalidate the cache tags not only when updating, but also when creating,     // because a language config override object uses the same cache tag as the     // default configuration object. Hence creating a language override is like     // an update of configuration, but only for a specific language.     Cache::invalidateTags($this->getCacheTags());
    $this->isNew = FALSE;
    $this->eventDispatcher->dispatch(new LanguageConfigOverrideCrudEvent($this), LanguageConfigOverrideEvents::SAVE_OVERRIDE);
    
Home | Imprint | This part of the site doesn't use cookies.