You are here

public function ConfigurableTrait::configErrors in Backup and Migrate 8.4

Get any validation errors in the config.

Parameters

array $params:

Return value

array

File

lib/backup_migrate_core/src/Config/ConfigurableTrait.php, line 118

Class

ConfigurableTrait
Class ConfigurableTrait.

Namespace

BackupMigrate\Core\Config

Code

public function configErrors($params = []) {
  $out = [];

  // Do some basic validation based on length and regex matching.
  $schema = $this
    ->configSchema($params);

  // Check that the schema is not empty.
  if (!empty($schema)) {

    // Check each specified field.
    foreach ($schema['fields'] as $key => $field) {
      $value = $this
        ->confGet($key);

      // Check if it's required.
      if (!empty($field['required']) && empty($value)) {
        $out[] = new ValidationError($key, $this
          ->t('%title is required.'), [
          '%title' => $field['title'],
        ]);
      }

      // Check it for length.
      if (!empty($field['min_length']) && strlen($value) < $field['min_length']) {
        $out[] = new ValidationError($key, $this
          ->t('%title must be at least %count characters.'), [
          '%title' => $field['title'],
          '%count' => $field['min_length'],
        ]);
      }
      if (!empty($field['max_length']) && strlen($value) > $field['max_length']) {
        $out[] = new ValidationError($key, $this
          ->t('%title must be at no more than %count characters.'), [
          '%title' => $field['title'],
          '%count' => $field['max_length'],
        ]);
      }

      // Check for the regular expression match.
      if (!empty($field['must_match']) && !preg_match($field['must_match'], $value)) {
        if (!empty($field['must_match_error'])) {
          $out[] = new ValidationError($key, $field['must_match_error'], [
            '%title' => $field['title'],
          ]);
        }
        else {
          $out[] = new ValidationError($key, $this
            ->t('%title contains invalid characters.'), [
            '%title' => $field['title'],
          ]);
        }
      }
    }
  }
  return $out;
}