0

I have this file which checks unique mobile no in the customer registration page and throws error if the mobile no entered is already in use

    <?php
namespace Vendor\Module\Model\Attribute\Backend;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;

class Mobile extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
{
    public function __construct(
      \Magento\Framework\Module\Manager $moduleManager
    ) {
        $this->moduleManager = $moduleManager;
    }
    protected function checkUniquePhone($object)
    {
        $attribute = $this->getAttribute();
        $entity = $attribute->getEntity();
        $attributeValue = $object->getData($attribute->getAttributeCode());
        $increment = null;
        if (!$entity->checkAttributeUniqueValue($attribute, $object)) {
        throw new NoSuchEntityException(__('Account with mobile number already exist.'));
        $exceptionThrown=true;
    }
    else $exceptionThrown=false; 
        }

    }       
   public function beforeSave($object)
    {
        if ($this->moduleManager->isOutputEnabled('Vendor_Module')) {
            $this->checkUniquePhone($object);
        }
        return parent::beforeSave($object);
    }

}

Now, I want to add a link to the forgot password page with exception message. But the it shows the link as a plain text. So i assumed maybe if I inject messagemanager it will work, but it doesn't. So I write a plugin instead. etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<!--plugin-->
<type name="Vendor\Module\Model\Attribute\Backend\Mobile">
  <plugin name="errorMessage" type="Vendor\Module\Plugin\error" sortOrder="1"/>
</type>

Plugin/error.php

 <?php
namespace JinHangCustomized\MagedelightSMSProfile\Plugin;

class error {
    protected $_messageManager;
    public function __construct(
        \Magento\Framework\Message\ManagerInterface $messageManager
    ) {
        $this->_messageManager = $messageManager;
    }
    public function aftercheckUniquePhone (\JinHangCustomized\MagedelightSMSProfile\Model\Attribute\Backend\Mobile $subject, $result)    
    {
        if ($subject->exceptionThrown)
        {
            $message = __(
                'There is already an account with this address.'
            );
            return $this->messageManager->addError($message);
        }
    }
}

Any idea how I can add a link to the exception message? Thank you.!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.