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>

2 Answers 2


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


<?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"  />


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('*')
            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            $this->_messageManager->addError('The value of "Sponsor Name" already exist.');
            return $resultRedirect;
            return true;



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:


Your Answer

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

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