How can I break a flow when some condition is true? Let's say the customer is writing a review for the product https://url/review/product/post/id/2/ and when the user presses the save butto some condition should be tested and if true the save process should not continue, and the user should be redirected to the same page with a error message ('your message could not be saved ....'.

For the moment I have the following code inside the observer:

public function execute(Observer $observer)

        if($sometest === true){
            $this->messageManager->addError(__("you have an error in your string"));
        //Avoid save and redirect to the same page

What should I use in order to break the save process and redirect the user to the same page?

1 Answer 1



I am not sure why review_controller_product_init_before . It is very strange to me. I have tried with redirection using Magento\Framework\Controller\ResultFactory and want to failed.

So, for your case, you can use direct using

observer over controller_action_predispatch_review_product_post

OR, You can create redirection using plugin over Magento\Review\Controller\Product\Post:execute

I have chosen the observer for your requirement.

Observer Class look Like:


namespace {VendorName}\{ModuelName}\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class RedirectfromPostObserver implements ObserverInterface {

     * @var \Magento\Framework\App\ActionFlag
    private $actionFlag;

     * @var \Magento\Framework\App\Action\Context
    private $context;

     * @var \Magento\Framework\App\Response\RedirectInterface
    private $redirect;

     * @var \Magento\Framework\Message\ManagerInterface
    private $manager;

    public function __construct(
            \Magento\Framework\App\Response\RedirectInterface $redirect,
            \Magento\Framework\Message\ManagerInterface $manager,
            \Magento\Framework\App\ActionFlag $actionFlag
    ) {

        $this->redirect = $redirect;
        $this->manager = $manager;
        $this->actionFlag = $actionFlag;

    public function execute(Observer $observer) {

        $controllerAction = $observer->getEvent()->getControllerAction();
        $currentActionName = $controllerAction->getRequest()->getActionName();
        $nickname = $controllerAction->getRequest()->getParam('nickname', false);
        $title = $controllerAction->getRequest()->getParam('title', false);
        $detail = $controllerAction->getRequest()->getParam('detail', false);
        $ratings = $controllerAction->getRequest()->getParam('ratings', false);

        $sometest = true;
        if ($sometest === true) {
            $this->manager->addErrorMessage(__("you have an error in your string %1", 
            $this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
            $this->redirect->redirect($controllerAction->getResponse(), $this->redirect->getRefererUrl());

  • Hi Amt, this line is generating an error $resultRedirect = $this->resultRedirect->create(ResultFactory::TYPE_REDIRECT); // Exception #0 (RuntimeException): Source class "\Exercise\Two\Observer\Result" for "Exercise\Two\Observer\ResultFactory" generation does not exist.
    – IgorAlves
    Feb 7, 2019 at 18:28
  • In fact there is two choices: 1 - use Magento\Framework\Controller\ResultFactory; and we can use the ResultFactory::TYPE_REDIRECT inside create or 2 - this dirty opition $resultRedirect = $this->resultRedirect->create('redirect'); At least will not break the process. I have used the first one. It is grabbing the referece URL.
    – IgorAlves
    Feb 7, 2019 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.