{"id":25234,"date":"2024-08-24T08:19:54","date_gmt":"2024-08-24T08:19:54","guid":{"rendered":"https:\/\/ecommerce.folio3.com\/blog\/?p=25234"},"modified":"2024-08-24T08:19:54","modified_gmt":"2024-08-24T08:19:54","slug":"how-to-create-custom-form-in-magento-2","status":"publish","type":"post","link":"https:\/\/ecommerce.folio3.com\/blog\/how-to-create-custom-form-in-magento-2\/","title":{"rendered":"How To Create Custom Form In Magento 2 &#8211; Step By Step Guide"},"content":{"rendered":"<h2><b>Introduction<\/b><\/h2>\n<p>Creating custom forms in Magento 2 is a powerful way to gather user input and manage data effectively within your online store. Whether you need a contact form, a survey, or a specialized data entry form, understanding the process of building custom forms can significantly enhance your store&#8217;s functionality. In this How To Create Custom Form In Magento 2 &#8211; Step By Step Guide, we will walk you through the entire process, from setting up the necessary modules to handling form submissions and managing the stored data.<\/p>\n<p><span style=\"font-weight: 400;\">Magento 2&#8217;s modular architecture makes it relatively straightforward to create custom forms that integrate seamlessly with your existing setup. By following this comprehensive guide, you&#8217;ll not only learn the technical steps involved but also gain insights into best practices that will ensure your custom forms are secure, user-friendly, and optimized for performance.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Custom forms can play a crucial role in enhancing user experience and driving engagement on your website. Whether you&#8217;re a developer looking to expand your Magento 2 skills or a store owner seeking to implement custom functionality, this guide is tailored to help you achieve your goals. Let\u2019s dive into the details of creating a custom form in Magento 2.<\/span><\/p>\n<p style=\"text-align: center;\"><strong>Read Also:<\/strong> <a href=\"https:\/\/ecommerce.folio3.com\/blog\/fourteen-reasons-why-you-need-to-build-your-ecommerce-store-with-magento\/\">Creating an Online Store from Scratch with Magento<\/a><\/p>\n<h3><b>Step 1: Create the Module Structure<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">First, let&#8217;s create the basic directory structure for our module:<\/span><\/p>\n<pre><b>app\/code\/Folio3\/CustomForm<\/b><\/pre>\n<p><span style=\"font-weight: 400;\">Inside the <\/span><b>CustomForm<\/b><span style=\"font-weight: 400;\"> directory, create the following folders<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">etc<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">view\/frontend\/layout<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Block<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">view\/frontend\/templates<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Controller<\/span><\/li>\n<\/ul>\n<h3><b>Step 2: Declare the Module<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">To declare the module, we need to create a <\/span><span style=\"font-weight: 400;\">module.xml<\/span><span style=\"font-weight: 400;\"> file under <\/span><span style=\"font-weight: 400;\">etc\/<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Xml<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">&lt;?xml version=\"1.0\"?&gt;<\/span>\r\n<span style=\"font-weight: 400;\">&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\"&gt;<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0&lt;module name=\"Folio3_CustomForm\" setup_version=\"1.0.0\"\/&gt;<\/span>\r\n<span style=\"font-weight: 400;\">&lt;\/config&gt;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Next, register the module in <\/span><span style=\"font-weight: 400;\">registration.php<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">&lt;?php<\/span>\r\n<span style=\"font-weight: 400;\">\\Magento\\Framework\\Component\\ComponentRegistrar::register(<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0'Folio3_CustomForm',<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0__DIR__<\/span>\r\n<span style=\"font-weight: 400;\">);<\/span><\/pre>\n<h3><b>Step 3: Create the Form Layout<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Now, we&#8217;ll define the layout XML to load our custom form. Create a new file <\/span><span style=\"font-weight: 400;\">customform_index_index.xml<\/span><span style=\"font-weight: 400;\"> under <\/span><span style=\"font-weight: 400;\">view\/frontend\/layout\/<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\"&gt; &lt;body&gt; &lt;referenceContainer name=\"content\"&gt; &lt;block class=\"Folio3\\CustomForm\\Block\\Form\" name=\"folio3.customform.form\" template=\"form.phtml\"\/&gt; &lt;\/referenceContainer&gt; &lt;\/body&gt; &lt;\/page&gt;<\/span><\/pre>\n<h3><b>Step 4: Create the Form Block<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">In the <\/span><span style=\"font-weight: 400;\">Block\/<\/span><span style=\"font-weight: 400;\"> directory, create a file <\/span><span style=\"font-weight: 400;\">Form.php<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">&lt;?php<\/span>\r\n<span style=\"font-weight: 400;\">namespace Folio3\\CustomForm\\Block;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">use Magento\\Framework\\View\\Element\\Template;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">class Form extends Template<\/span>\r\n\r\n<span style=\"font-weight: 400;\">{<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0public function getFormAction()<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0{<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return $this-&gt;getUrl('customform\/index\/post');<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0}<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<h3><b>Step 5: Create the Form Template<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Now, create the form template <\/span><span style=\"font-weight: 400;\">form.phtml<\/span><span style=\"font-weight: 400;\"> in <\/span><span style=\"font-weight: 400;\">view\/frontend\/templates\/<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">&lt;form action=\"&lt;?= $block-&gt;getFormAction(); ?&gt;\" method=\"post\"&gt;<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0&lt;div&gt;<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;label for=\"name\"&gt;Name&lt;\/label&gt;<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;input type=\"text\" name=\"name\" id=\"name\" required&gt;<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0&lt;\/div&gt;<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0&lt;div&gt;<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;label for=\"email\"&gt;Email&lt;\/label&gt;<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;input type=\"email\" name=\"email\" id=\"email\" required&gt;<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0&lt;\/div&gt;<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0&lt;div&gt;<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;button type=\"submit\"&gt;Submit&lt;\/button&gt;<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0&lt;\/div&gt;<\/span>\r\n<span style=\"font-weight: 400;\">&lt;\/form&gt;\r\n<\/span><\/pre>\n<h3><b>Step 6: Handle Form Submission<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Create a controller to handle form submission in <\/span><span style=\"font-weight: 400;\">Controller\/Index\/Post.php<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">&lt;?php<\/span>\r\n<span style=\"font-weight: 400;\">namespace Folio3\\CustomForm\\Controller\\Index;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">use Magento\\Framework\\App\\Action\\Action;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">use Magento\\Framework\\App\\Action\\Context;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">use Magento\\Framework\\Controller\\ResultFactory;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">class Post extends Action<\/span>\r\n<span style=\"font-weight: 400;\">{<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0protected $resultFactory;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public function __construct(<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Context $context,<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ResultFactory $resultFactory<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$this-&gt;resultFactory = $resultFactory;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0parent::__construct($context);<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public function execute()<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$post = $this-&gt;getRequest()-&gt;getPostValue();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (!$post) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return $this-&gt;_redirect('*\/*\/');<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$resultRedirect = $this-&gt;resultFactory-&gt;create(ResultFactory::TYPE_REDIRECT);<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$resultRedirect-&gt;setUrl($this-&gt;_redirect-&gt;getRefererUrl());<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ Handle your form data here<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$this-&gt;messageManager-&gt;addSuccessMessage('Form submitted successfully.');<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return $resultRedirect;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<h3><b>Step 7: Add Routes<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Define the route for your form submission in <\/span><span style=\"font-weight: 400;\">etc\/frontend\/routes.xml<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"font-weight: 400;\">&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:App\/etc\/routes.xsd\"&gt;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;router id=\"standard\"&gt;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;route id=\"customform\" frontName=\"customform\"&gt;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;module name=\"Folio3_CustomForm\" \/&gt;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/route&gt;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;\/router&gt;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">&lt;\/config&gt;<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h3><b>Step 8: Test Your Custom Form<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Finally, flush the cache and test your custom form by navigating to the following URL:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">yourstore.com\/customform\/index\/index<\/span><\/pre>\n<h2><b>Conclusion<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">You should see your custom form displayed on this page, and upon submission, it will handle the data as configured. Depending on your needs, you can extend the functionality to either send an email notification to the admin user or save the form data directly into the database.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For example, if you want to notify the admin when a user submits the form, you can configure Magento 2 to trigger an email with all the form details. This is particularly useful for contact forms or any submission where immediate action is required.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Alternatively, if you prefer to store the data for later processing, you can save the records in the database using Magento 2\u2019s model and resource model system. This approach is ideal for collecting survey responses, user feedback, or any information that you might want to analyze or export at a later time. The flexibility of Magento 2 allows you to choose the method that best suits your business requirements.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">By integrating these functionalities, your custom form becomes a powerful tool for gathering and managing user input effectively, enhancing the overall capability of your Magento 2 store.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Creating custom forms in Magento 2 is a powerful way to gather user input and manage data effectively within your online store. Whether you need a contact form, a survey, or a specialized data entry form, understanding the process of building custom forms can significantly enhance your store&#8217;s functionality. In this How To Create<\/p>\n","protected":false},"author":57,"featured_media":25235,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[37],"class_list":{"0":"post-25234","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-uncategorized","8":"tag-magento"},"acf":[],"featured_image_data":{"src":"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2024\/08\/Blue-White-Modern-Marketing-Strategy-Blog-Banner-12.jpg","alt":"magento","caption":""},"_links":{"self":[{"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/posts\/25234"}],"collection":[{"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/users\/57"}],"replies":[{"embeddable":true,"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/comments?post=25234"}],"version-history":[{"count":0,"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/posts\/25234\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/media\/25235"}],"wp:attachment":[{"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/media?parent=25234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/categories?post=25234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/tags?post=25234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}