I have an ajax customer registration form with native captcha. I am already returning ajax based response against other field validations. Only thing left is Captcha validation.

Please tell me how can I validate this captcha remaining with in the form posted controller.


So whenever you are sending a call to ajax controller then you have to make a another call to check the validation for that you have to create a plugin. This plugin will execute when you call your controller.

Assuming you already have a controller Controller/Ajax/Login.php - This is my controller

Create etc/di.xml

    <?xml version="1.0"?>
    * Copyright © Magento, Inc. All rights reserved.
    * See COPYING.txt for license details.
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Usermg\Register\Controller\Ajax\Login">
        <plugin name="login_ajax" type="Usermg\Register\Plugin\AjaxLogin" sortOrder="50" />


In this type name is your controller name and inside tag is for plugin name and plugin path

Create Plugin/AjaxLogin.php

     * Copyright © Magento, Inc. All rights reserved.
     * See COPYING.txt for license details.
    namespace Usermg\Register\Plugin;

    use Magento\Captcha\Helper\Data as CaptchaHelper;
    use Magento\Framework\Session\SessionManagerInterface;
    use Magento\Framework\Controller\Result\JsonFactory;

    class AjaxLogin
         * @var \Magento\Captcha\Helper\Data
        protected $helper;

         * @var \Magento\Framework\Session\SessionManagerInterface
        protected $sessionManager;

         * @var JsonFactory
        protected $resultJsonFactory;

        protected $captchaStringResolver;

         * @param CaptchaHelper $helper
         * @param SessionManagerInterface $sessionManager
         * @param JsonFactory $resultJsonFactory
         * @throws \RuntimeException
        public function __construct(
            CaptchaHelper $helper,
            SessionManagerInterface $sessionManager,
            JsonFactory $resultJsonFactory,
             \Magento\Captcha\Observer\CaptchaStringResolver $captchaStringResolver //
        ) {
            $this->helper = $helper;
            $this->sessionManager = $sessionManager;
            $this->resultJsonFactory = $resultJsonFactory;
            $this->captchaStringResolver = $captchaStringResolver;

         * @param \Usermg\Register\Controller\Ajax\Login $subject
         * @param \Closure $proceed
         * @return $this
         * @SuppressWarnings(PHPMD.NPathComplexity)
         * @SuppressWarnings(PHPMD.CyclomaticComplexity)
        public function aroundExecute(
            \Usermg\Register\Controller\Ajax\Login $subject,
            \Closure $proceed
        ) {
            $formId = 'captcha_login'; 
            $captchaModel = $this->helper->getCaptcha($formId);
            $result = $this->resultJsonFactory->create();

            if (!$captchaModel->isCorrect($this->captchaStringResolver->resolve($subject->getRequest(), $formId))) {
                $response = [
                    'errors' => true,
                    'message' => __('Incorrect CAPTCHA.')
                return $result->setData($response);
            return $proceed();

This plugin will inject when your controller run your execute funtion aroundExecute - this will send error if catcha is wrong and will stop the further process. 2) if captcha is fine then it will jump to your controller logic.

Hope this will help.

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.