In this blog, we are going to learn how we can handle validations in modern admin form handler from module based on Symfony.
We handle validations to protect our application from state inconsistency, failures, security-related issues, etc.
In modern pages, Controllers have or should have only one responsibility: handle the request and return a response. This is why in modern pages, controllers should be as thin as possible and rely on specific classes (services) to manage the data.
But, Sometimes we use some modern hooks like actionAfterUpdateCustomerFormHandler
, and actionAfterCreateCustomerFormHandler
in our module and we need to handle validations and show those error messages before the request reaches the modern controller.
In the above case, we use ModuleErrorException which is located in the directory src/Core/Module/Exception/ModuleErrorException.php
Below is the example to use this.
public function hookActionAfterUpdateCustomerFormHandler(array $params) { $message = $this->getTranslator()->trans( 'Some valdation error message', [], 'Admin.Notifications.Error' ); throw new \PrestaShop\PrestaShop\Core\Module\Exception\ModuleErrorException($message); }
The output will be as below
We can also use ModuleErrorException
in other hooks like actionOrderStatusUpdate
if we want to add validation before updating order status.
Below is the example to use this
public function hookActionOrderStatusUpdate($params) { $message = $this->getTranslator()->trans( 'Some validation error message', [], 'Admin.Notifications.Error' ); }
That’s all about this blog. Hope it will help you.
If you are facing any issues or have any doubts about the above process, please feel free to contact us through the comment section.
Also, you can explore our PrestaShop Development Services and a large range of quality PrestaShop Modules.
For any doubt contact us at [email protected]
Be the first to comment.