0

I Added a custom sponsor name field on magento2 account creation form and done client side validation also. How can I do server side validation??? Need to check sponsor is available in database or not.

<div class="field required">
    <legend class="legend"><span><?php echo $block->escapeHtml(__('Sponsor Information')) ?></span></legend>
    <label for="sponsor_name" class="label"><span><?php /* @escapeNotVerified */ echo __('Sponsor Name') ?></span></label>
    <div class="control">
        <input type="text" name="sponsor_name" id="sponsor_name" title="<?php /* @escapeNotVerified */ echo __('Sponsor name') ?>" class="input-text required-entry validate-length minimum-length-5 v-validate"  autocomplete="off" value="" >
        <div class="sponsor-msg-div"></div>
    </div>
</div>
0

i had done validation using plugin before create account execute function.

..\etc\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\Controller\Account\CreatePost">
        <plugin name="Kblikruta_Customerattr_Plugin" type="Kblikruta\Customerattr\Plugin\Checkattribute" sortOrder="10" disabled="false"  />
    </type>
</config>

..\Plugin\Checkattribute.php

namespace Kblikruta\Customerattr\Plugin;

use Magento\Framework\Controller\ResultFactory;

use Magento\Framework\Message\ManagerInterface;

use Magento\Framework\App\RequestInterface;


class Checkattribute
{

    public function __construct(
        ResultFactory $Redirect, 
        ManagerInterface $messageManager,   
        RequestInterface $request
    )
    {
        $this->resultFactory = $Redirect;
        $this->_messageManager = $messageManager;
        $this->getRequest = $request;
    }

    public function beforeExecute(\Magento\Customer\Controller\Account\CreatePost $subject)
    {

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customerObj = $objectManager->create('Magento\Customer\Model\ResourceModel\Customer\Collection');
        $collection = $customerObj->addAttributeToSelect('*')
                  ->addAttributeToFilter('sponsor_name',$this->getRequest->getParam('sponsor_name'))
                  ->load();
        $customerdata=$collection->getData();
        if(!empty($customerdata))
        {
            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            $this->_messageManager->addError('The value of "Sponsor Name" already exist.');
            $resultRedirect->setPath('customer/account/');
            $this->getRequest->setParam('form_key','');
            return $resultRedirect;
        }
        else
        {
            return true;
        }

    }

}
0

Unfortunately I cannot comment, so I will try a response. The solution proposed by snkt worked but it does not stop the form from executing and adding the customer. Do you know how can I add the error message and also prevent from adding the customer ?

Thanks !

================

I added the following line to solve it if the validation returns false:

        $this->getRequest->setParam('email_address','');

Your Answer

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

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