From cb6528902da634a553f7cf7e74a283c70d89d5b2 Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Tue, 2 Jan 2024 20:48:44 +0100 Subject: [PATCH] Validation for step 2 --- src/controllers/ElementController.php | 11 +++++++++- src/elements/ExportElement.php | 29 ++++++++++++++++++++++--- src/templates/sprig/element/step_2.twig | 6 +++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/controllers/ElementController.php b/src/controllers/ElementController.php index a7fd4a0..ed829cf 100644 --- a/src/controllers/ElementController.php +++ b/src/controllers/ElementController.php @@ -167,7 +167,7 @@ public function actionStep1() $export->settings = Json::encode(array_merge($export->getSettings(), $body['settings'])); - $export->setScenario('step1'); + $export->setScenario(ExportElement::STEP_1); $export->validate(); if ($export->getErrors()) { @@ -200,7 +200,16 @@ public function actionStep2() $fields = array_filter($body['fields']); $export->fields = Json::encode($fields); + $export->setScenario(ExportElement::STEP_2); + + $export->validate(); + $selectedFields = $export->getSelectedFields(); + + if(!$selectedFields) { + $export->addError('fields', Craft::t('exporter', 'Please select at least one field to export')); + } + if ($export->getErrors()) { /** @phpstan-ignore-next-line */ Craft::$app->getUrlManager()->setRouteParams([ diff --git a/src/elements/ExportElement.php b/src/elements/ExportElement.php index 7db15dd..2c89e26 100644 --- a/src/elements/ExportElement.php +++ b/src/elements/ExportElement.php @@ -28,6 +28,10 @@ class ExportElement extends Element public $runSettings; + public const STEP_1 = "step1"; + + public const STEP_2 = "step2"; + /** * @inheritdoc */ @@ -137,7 +141,8 @@ public function canDelete(User $user): bool public function scenarios() { return [ - 'step1' => ['name', 'group'], + self::STEP_1 => ['name', 'group'], + self::STEP_2 => [], 'default' => [], ]; } @@ -150,14 +155,30 @@ public function scenarios() public function validate($attributeNames = null, $clearErrors = true): bool { - if ($this->scenario == 'step1') { + if ($this->scenario === self::STEP_1) { $settings = $this->getSettings(); if (!$settings['group']) { - $this->addError("group", "Group cannot be blank"); + $this->addError("group", Craft::t('exporter', 'Group cannot be blank')); + } + } + if ($this->scenario === self::STEP_2) { + $fields = $this->getSelectedFields(); + if(!$fields) { + $this->addError('fields', Craft::t('exporter', 'Please select at least one field to export')); } } + return parent::validate(); + } + public function getSelectedFields(): array + { + return array_filter($this->getFields(), function ($field) { + if($field['handle']) { + return true; + } + return false; + }); } @@ -259,6 +280,8 @@ public function getFields(): null|array return Json::decode($this->fields); } + + public function getRunSettings(): null|array { return Json::decode($this->runSettings) ?? []; diff --git a/src/templates/sprig/element/step_2.twig b/src/templates/sprig/element/step_2.twig index afca5c7..c3d7ed1 100644 --- a/src/templates/sprig/element/step_2.twig +++ b/src/templates/sprig/element/step_2.twig @@ -53,6 +53,12 @@

{{ "Which custom fields do you want to include in this export?"|t('exporter') }}

+ {% if errors is defined %} + {% if errors.fields is defined %} +
{{ errors.fields|first }}
+ {% endif %} + {% endif %} + {% set allFields = export.getSupportedFields(element) %} {% set showNotSupportedNotice = false %} {% for field in allFields %}