Bhaveshpp

Professional Magento Developr - Healp eachother to grow

Magento2: How to add recapcha in custom form

27 May 2021 » magento2

Add Form Id in etc/config.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
   <default>
       <captcha translate="label">
            <frontend>
                <areas>
                    <unsubscribe_form> <!-- Change form Id here -->
                        <label>Unsubscribe form</label>
                    </unsubscribe_form>
                </areas>
            </frontend>
        </captcha>
        <customer>
            <captcha>
                <shown_to_logged_in_user>
                    <unsubscribe_form>1</unsubscribe_form> <!-- Change form Id here -->
                </shown_to_logged_in_user>
                <always_for>
                    <unsubscribe_form>1</unsubscribe_form> <!-- Change form Id here -->
                </always_for>
            </captcha>
        </customer>
   </default>
</config>

Add capcha field in form layout xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceContainer name="content">
        <block class="Bhaveshpp\Unsubscribe\Block\Unsubscribe" name="unsubscribe-form" template="Bhaveshpp_Unsubscribe::unsubscribe-form.phtml" >
        	<container name="form.additional.info.unsubcribe" label="Form Additional Info">
        		<block class="Magento\Captcha\Block\Captcha" name="captcha.unsubcribe" after="-" cacheable="false">
	                <action method="setFormId">
	                    <argument name="formId" xsi:type="string">unsubscribe_form</argument>
	                </action>
	                <action method="setImgWidth">
	                    <argument name="width" xsi:type="string">230</argument>
	                </action>
	                <action method="setImgHeight">
	                    <argument name="width" xsi:type="string">50</argument>
	                </action>
	            </block>
        	</container>
        </block>
    </referenceContainer>
    <referenceBlock name="head.components">
        <block class="Magento\Framework\View\Element\Js\Components" name="captcha_page_head_components" template="Magento_Captcha::js/components.phtml"/>
    </referenceBlock>
</page>

validate capcha


        /**
         * \Magento\Captcha\Helper\Data $captchaHelper
         * 
        */
         $formId = 'unsubscribe_form';
            $captcha = $this->_captchaHelper->getCaptcha($formId);
            if ($captcha->isRequired()) {
                if (!$captcha->isCorrect($this->_captchaStringResolver->resolve($this->getRequest(), $formId))) 
                {        
                    $this->messageManager->addError(__('Incorrect CAPTCHA.'));
                    $this->getDataPersistor()->set($formId, $this->getRequest()->getPostValue());
                    $this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
                    $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
                }else{
                    $email = (string)$this->getRequest()->getPost('email');
                    $this->nowFinalyUnsubscribe($email);
                }
            }