Skip to content

Commit

Permalink
Fixed YAML blueprint valdiation/filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Feb 15, 2018
1 parent ee8ea5a commit 84a2a08
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
1. [](#improved)
* Better `Page.collection()` filtering support including ability to have non-published pages in collections
* Stopped Chrome from auto-completing admin user profile form [#1847](https://github.com/getgrav/grav/issues/1847)
1. [](#bugfix)
1. [](#bugfix)
* Properly validate YAML blueprint fields so admin can save as proper YAML now [addresses many issues]
* Fixed OpenGraph metatags so only Twitter uses `name=`, and all others use `property=` [#1849](https://github.com/getgrav/grav/issues/1849)
* Fixed an issue with `evaluate()` and `evaluate_twig()` Twig functions that throws invalid template error
* Fixed issue with `|sort_by_key` twig filter if the input was null or not an array
Expand Down
38 changes: 17 additions & 21 deletions system/src/Grav/Common/Data/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ public static function validate($value, array $field)
$field['type'] = 'text';
}

// If this is a YAML field, stop validation
if (isset($field['yaml']) && $field['yaml'] === true) {
return $messages;
}

// Get language class.
$language = Grav::instance()['language'];

Expand All @@ -54,6 +49,12 @@ public static function validate($value, array $field)
? $language->translate($field['validate']['message'])
: $language->translate('FORM.INVALID_INPUT', null, true) . ' "' . $language->translate($name) . '"';


// If this is a YAML field validate/filter as such
if ($type != 'yaml' && isset($field['yaml']) && $field['yaml'] === true) {
$method = 'typeYaml';
}

if (method_exists(__CLASS__, $method)) {
$success = self::$method($value, $validate, $field);
} else {
Expand Down Expand Up @@ -100,15 +101,16 @@ public static function filter($value, array $field)
$field['type'] = 'text';
}

// If this is a YAML field, simply parse it and return the value.
if (isset($field['yaml']) && $field['yaml'] === true) {
return $value;
}

// Validate type with fallback type text.
$type = (string) isset($field['validate']['type']) ? $field['validate']['type'] : $field['type'];
$method = 'filter' . ucfirst(strtr($type, '-', '_'));

// If this is a YAML field validate/filter as such
if ($type != 'yaml' && isset($field['yaml']) && $field['yaml'] === true) {
$method = 'filterYaml';
}

if (!method_exists(__CLASS__, $method)) {
$method = 'filterText';
}
Expand Down Expand Up @@ -639,22 +641,16 @@ protected static function filterList($value, array $params, array $field)
return (array) $value;
}

public static function typeYaml($value, $params)
{
try {
Yaml::parse($value);
return true;
} catch (ParseException $e) {
return false;
}
}

public static function filterYaml($value, $params)
{
try {
return (array) Yaml::parse($value);
if (is_string($value)) {
return (array) Yaml::parse($value);
} else {
return $value;
}
} catch (ParseException $e) {
return null;
return $value;
}
}

Expand Down

0 comments on commit 84a2a08

Please sign in to comment.