Skip to content

Commit

Permalink
Add formater for validator errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Quetzacoalt91 committed Nov 11, 2024
1 parent e4b50a7 commit 7fc3be6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
25 changes: 25 additions & 0 deletions classes/Twig/ValidatorToFormFormater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace PrestaShop\Module\AutoUpgrade\Twig;

abstract class ValidatorToFormFormater
{
/**
* @param array<array{message:string, target?:string}> $errors
*
* @return array<'global'|string, string>
*/
public static function format($errors): array
{
return array_column(
array_map(function ($error) {
return [
'key' => $error['target'] ?? 'global',
'value' => $error['message'],
];
}, $errors),
'value',
'key'
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use PrestaShop\Module\AutoUpgrade\Router\Routes;
use PrestaShop\Module\AutoUpgrade\Twig\PageSelectors;
use PrestaShop\Module\AutoUpgrade\Twig\UpdateSteps;
use PrestaShop\Module\AutoUpgrade\Twig\ValidatorToFormFormater;
use Symfony\Component\HttpFoundation\JsonResponse;

class UpdatePageUpdateOptionsController extends AbstractPageWithStepController
Expand Down Expand Up @@ -65,9 +66,9 @@ public function saveOption(): JsonResponse
$name => $this->request->request->get('value'),
];

$error = $this->upgradeContainer->getConfigurationValidator()->validate($config);
$errors = $this->upgradeContainer->getConfigurationValidator()->validate($config);

if (empty($error)) {
if (empty($errors)) {
if ($name === 'PS_DISABLE_OVERRIDES') {
$this->upgradeContainer->initPrestaShopCore();
UpgradeConfiguration::updatePSDisableOverrides($this->request->request->getBoolean('value'));
Expand All @@ -77,7 +78,10 @@ public function saveOption(): JsonResponse
}
}

return $this->getRefreshOfForm(array_merge($this->getParams(), ['error' => $error]));
return $this->getRefreshOfForm(array_merge(
$this->getParams(),
['errors' => ValidatorToFormFormater::format($errors)]
));
}

public function submit(): JsonResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use PrestaShop\Module\AutoUpgrade\Services\PhpVersionResolverService;
use PrestaShop\Module\AutoUpgrade\Twig\PageSelectors;
use PrestaShop\Module\AutoUpgrade\Twig\UpdateSteps;
use PrestaShop\Module\AutoUpgrade\Twig\ValidatorToFormFormater;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use PrestaShop\Module\AutoUpgrade\Upgrader;
use PrestaShop\Module\AutoUpgrade\UpgradeSelfCheck;
Expand Down Expand Up @@ -223,24 +224,13 @@ public function save(): JsonResponse
$config->merge($requestConfig);

$this->upgradeContainer->getUpgradeConfigurationStorage()->save($config, UpgradeFileNames::CONFIG_FILENAME);
} else {
$errors = array_column(
array_map(function ($error) {
return [
'key' => $error['target'] ?? 'global',
'value' => $error['message'],
];
}, $errors),
'value',
'key'
);
}

$params = array_merge(
$this->getParams(),
[
'current_values' => $requestConfig,
'errors' => $errors,
'errors' => ValidatorToFormFormater::format($errors),
]
);

Expand Down
6 changes: 3 additions & 3 deletions views/templates/steps/update-options.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
description: "All the modules installed after creating your store are considered non-native modules. They might be " ~
"incompatible with the new version of PrestaShop. We recommend deactivating them during the update."|trans({}),
value: form_fields.deactive_non_native_modules.value,
error_message: error[form_fields.deactive_non_native_modules.field] is defined ? error[form_fields.deactive_non_native_modules.field] : null,
error_message: errors[form_fields.deactive_non_native_modules.field] is defined ? errors[form_fields.deactive_non_native_modules.field] : null,
} %}

{% include "@ModuleAutoUpgrade/components/render-switch.html.twig" with {
Expand All @@ -31,7 +31,7 @@
title: "Regenerate email templates"|trans({}),
description: "If you've customized email templates, your changes will be lost if you activate this option."|trans({}),
value: form_fields.regenerate_email_templates.value,
error_message: error[form_fields.regenerate_email_templates.field] is defined ? error[form_fields.regenerate_email_templates.field] : null,
error_message: errors[form_fields.regenerate_email_templates.field] is defined ? errors[form_fields.regenerate_email_templates.field] : null,
} %}

{% include "@ModuleAutoUpgrade/components/render-switch.html.twig" with {
Expand All @@ -42,7 +42,7 @@
"one method or as many as you need. This option disables all classes & controllers overrides, allowing " ~
"you to avoid conflicts during and after updates."|trans({}),
value: form_fields.disable_all_overrides.value,
error_message: error[form_fields.disable_all_overrides.field] is defined ? error[form_fields.disable_all_overrides.field] : null,
error_message: errors[form_fields.disable_all_overrides.field] is defined ? errors[form_fields.disable_all_overrides.field] : null,
} %}
</form>
{% endblock %}
Expand Down

0 comments on commit 7fc3be6

Please sign in to comment.