Question

I am using custom plugin to validate the user input for customer profile.

I have created Module Company_Module and create di.xml file in etc to verify the input given by the user:

<type name="Magento\Customer\Controller\Account\EditPost">

 <plugin name="restrictEditInfo" type="Company\Mymodule\Plugin\Controller\Customer\EditPost" />

</type>

For this in my EditPost plugin file i have given following code:

public function aroundExecute(
    \Magento\Customer\Controller\Account\EditPost $subject,
    \Closure $proceed
)
{


    $resultRedirect = $this->resultRedirectFactory->create();
    $requestParameters = $subject->getRequest()->getPostValue();

    if ($this->preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/',$requestParameters['firstname']))
    {                  
            $this->messageManager->addError(__('Special Characters are not allowed.'));
            return $resultRedirect->setRefererOrBaseUrl();
    }
 return $proceed();  

} 

This validates the input given by the user and prevent form saving if any special characters are given by the user.

Now issue is: I want to send modified output. It means if customer enter any special characters in input then i want to remove those tags and save it as normal string. To do so i am doing preg_replace which works fine but to save the modified data i am passing the $requestParameter in proceed method.

    return $proceed($requestParamaters);  

To save the modified data i am passing $requestparamaters In proceed method but it's not working.

To debug this i checked after this around method call is going into controller which is showing the same input given by the user not the modified output i given from the proceed method.

How can i pass my modified input to save? How can i pass modified output for save?

Any help would be appreciated!

Was it helpful?

Solution

The execute() method of the controller has no input parameter, therefore $proceed($requestParamaters) can't work. In the controller the data is taken from the post input parameter ($this->getRequest()->getPostValue()) therefore you have to change that data in your plugin.

You can use the method $subject->getRequest()setPostValue($name, $value) for that. For example for the firstname you can use

$subject->getRequest()->setPostValue('firstname', $requestParameters['firstname'])

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top