Reading list Switch to dark mode

    How to handle validations in modern admin form handler from module

    Updated 18 July 2022

    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

    Start your headless eCommerce
    now.
    Find out More

    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

    handle validations

    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'
        );
    }
    blog2

    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]

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home