Skip to content

Commit

Permalink
Validation for step 2
Browse files Browse the repository at this point in the history
  • Loading branch information
janhenckens committed Jan 2, 2024
1 parent 98ba62d commit cb65289
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/controllers/ElementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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([
Expand Down
29 changes: 26 additions & 3 deletions src/elements/ExportElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class ExportElement extends Element

public $runSettings;

public const STEP_1 = "step1";

public const STEP_2 = "step2";

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -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' => [],
];
}
Expand All @@ -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;
});
}


Expand Down Expand Up @@ -259,6 +280,8 @@ public function getFields(): null|array
return Json::decode($this->fields);
}



public function getRunSettings(): null|array
{
return Json::decode($this->runSettings) ?? [];
Expand Down
6 changes: 6 additions & 0 deletions src/templates/sprig/element/step_2.twig
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@

<div class="custom-fields">
<h2>{{ "Which custom fields do you want to include in this export?"|t('exporter') }}</h2>
{% if errors is defined %}
{% if errors.fields is defined %}
<div class="error">{{ errors.fields|first }}</div>
{% endif %}
{% endif %}
<input type="hidden" name="fields">
{% set allFields = export.getSupportedFields(element) %}
{% set showNotSupportedNotice = false %}
{% for field in allFields %}
Expand Down

0 comments on commit cb65289

Please sign in to comment.