-
-
Notifications
You must be signed in to change notification settings - Fork 2
Add captcha to custom form in magento 2
ProcessEight edited this page Mar 24, 2021
·
1 revision
\Magento\Captcha\Block\Captcha::toHtml renders the form
In this code sample, the block class is added to the form.additional.info
container alias.
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="head.components">
<block class="Magento\Framework\View\Element\Js\Components" name="captcha_page_head_components" template="Magento_Captcha::js/components.phtml"/>
</referenceBlock>
<referenceContainer name="content">
<block class="PavingDirect\TradeRegistration\Block\Form\Register"
name="customer_form_register"
template="PavingDirect_TradeRegistration::form/register.phtml"
cacheable="false">
<container name="form.additional.info" as="form_additional_info"/>
</block>
</referenceContainer>
<referenceContainer name="form.additional.info">
<block class="Magento\Captcha\Block\Captcha" name="captcha" after="-" cacheable="false">
<action method="setFormId">
<argument name="formId" xsi:type="string">user_create</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>
</referenceContainer>
</body>
</page>
The form.additional.info
container alias is added by this snippet inside the block class definition of the form we want to add CAPTCHA too:
<container name="form.additional.info" as="form_additional_info"/>
The formId
argument maps to the CAPTCHA form types defined in Admin, Stores, Configuration, Customers, Customer Configuration, CAPTCHA.