diff options
author | Victor S <32728904+ulcuber@users.noreply.github.com> | 2022-06-22 06:43:28 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-22 06:43:28 +0300 |
commit | b8c84b378527367ed5c3495f1f680d941200a8b5 (patch) | |
tree | e481f25442ddf1e42b5e2d93712b9deef8488943 /core | |
parent | c5483b79b322a4fa2601aed9beea7c478603d333 (diff) |
added: setting prepare callback before validation (#19363)
Diffstat (limited to 'core')
-rw-r--r-- | core/Settings/FieldConfig.php | 14 | ||||
-rw-r--r-- | core/Settings/Setting.php | 4 |
2 files changed, 18 insertions, 0 deletions
diff --git a/core/Settings/FieldConfig.php b/core/Settings/FieldConfig.php index a6480216b6..a74878757c 100644 --- a/core/Settings/FieldConfig.php +++ b/core/Settings/FieldConfig.php @@ -191,6 +191,20 @@ class FieldConfig public $inlineHelp = null; /** + * A closure that prepares the setting value. If supplied, this closure will be executed before + * the setting has been validated. + * + * **Example** + * + * $setting->prepare = function ($value, Setting $setting) { + * return mb_strtolower($value); + * } + * + * @var null|\Closure + */ + public $prepare = null; + + /** * A closure that does some custom validation on the setting before the setting is persisted. * * The closure should take two arguments: the setting value and the {@link Setting} instance being diff --git a/core/Settings/Setting.php b/core/Settings/Setting.php index d7b791fb2c..67ed0833e1 100644 --- a/core/Settings/Setting.php +++ b/core/Settings/Setting.php @@ -221,6 +221,10 @@ class Setting $config = $this->configureField(); + if ($config->prepare && $config->prepare instanceof \Closure) { + $value = call_user_func($config->prepare, $value, $this); + } + $this->validateValue($value); if ($config->transform && $config->transform instanceof \Closure) { |