diff --git a/framework/core/src/Settings/SettingsServiceProvider.php b/framework/core/src/Settings/SettingsServiceProvider.php index 4f12a8b15..942d842c9 100644 --- a/framework/core/src/Settings/SettingsServiceProvider.php +++ b/framework/core/src/Settings/SettingsServiceProvider.php @@ -10,7 +10,9 @@ namespace Flarum\Settings; use Flarum\Foundation\AbstractServiceProvider; +use Flarum\Settings\Event\Saving; use Illuminate\Contracts\Container\Container; +use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\ConnectionInterface; use Illuminate\Support\Collection; @@ -41,4 +43,14 @@ class SettingsServiceProvider extends AbstractServiceProvider $this->container->alias(SettingsRepositoryInterface::class, 'flarum.settings'); } + + public function boot(Dispatcher $events, SettingsValidator $settingsValidator) + { + $events->listen( + Saving::class, + function (Saving $event) use ($settingsValidator) { + $settingsValidator->assertValid($event->settings); + } + ); + } } diff --git a/framework/core/src/Settings/SettingsValidator.php b/framework/core/src/Settings/SettingsValidator.php new file mode 100644 index 000000000..cf73aa5be --- /dev/null +++ b/framework/core/src/Settings/SettingsValidator.php @@ -0,0 +1,61 @@ +globalRules; + }, $attributes); + + // Apply attribute specific rules. + foreach ($rules as $key => $value) { + if (array_key_exists($key, $this->rules)) { + $rules[$key] = array_merge($rules[$key], $this->rules[$key]); + } + } + + $validator = $this->validator->make($attributes, $rules, $this->getMessages()); + + foreach ($this->configuration as $callable) { + $callable($this, $validator); + } + + return $validator; + } +}