{"id":2496,"date":"2024-12-31T20:22:33","date_gmt":"2024-12-31T20:22:33","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/how-to-throw-exception-on-magento-2-admin-save-configuration\/"},"modified":"2025-05-21T16:32:14","modified_gmt":"2025-05-21T11:02:14","slug":"throw-exception-on-magento-2-admin-save-configuration","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/throw-exception-on-magento-2-admin-save-configuration\/","title":{"rendered":"How to Throw Exception on Magento 2 Admin Save Configuration"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Sometimes you may require to&nbsp;<em><strong>throw exception on Magento 2 admin save configuration<\/strong><\/em>. Here is the complete solution to do that.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose you have developed a custom&nbsp;<a href=\"https:\/\/meetanshi.com\/magento-2-extensions.html\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 module<\/a>&nbsp;that requires a third-party library or component to function properly. The extension may not work in case of the absence of such required components and may further affect the overall functionality of the store. Showing a simple exception in the backend during the module configuration part can prevent such an instance and help the admin solve the issue instantly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this blog post, I have provided a complete programmatic solution to restrict admin from saving configuration in Magento 2.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Throw Exception on Magento 2 Admin Save Configuration<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can use the following steps to throw an exception to the Magento 2 admin while saving the configuration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First of all, create a&nbsp;<em><strong>registration.php<\/strong><\/em>&nbsp;file at the&nbsp;<strong>app\/code\/Vendor\/Module\/<\/strong>&nbsp;directory and add the following code to register a custom module:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n \nuse Magento\\Framework\\Component\\ComponentRegistrar;\n \nComponentRegistrar::register(\n    ComponentRegistrar::MODULE,\n    'Vendor_Module',\n    __DIR__\n);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create a new module.xml file at app\/code\/Vendor\/Module\/etc\/ directory and add the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;type name=\"Magento\\Config\\Model\\Config\">\n      &lt;plugin name=\"admin_system_config_save_plugin\" type=\"Vendor\\Module\\Plugin\\ConfigPlugin\"\/>\n&lt;\/type><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Open the&nbsp;<em><strong>ConfigPlugin.php<\/strong><\/em>&nbsp;file at&nbsp;<strong>app\/code\/Vendor\/Module\/Plugin\/&nbsp;directory<\/strong>&nbsp;and add the following code to throw a custom exception in the Magento 2 admin panel:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n \n \nnamespace Vendor\\Vendor\\Plugin;\n \nuse Magento\\Framework\\Exception\\InputException;\nuse Magento\\Config\\Model\\Config;\nuse Magento\\Framework\\Message\\ManagerInterface;\n \nclass ConfigPlugin\n{\n public function aroundSave(\n Config $config,\n \\Closure $proceed\n ) {\n if (condition) {\n throw new InputException(__('Error Message'));\n }\n return $proceed();\n }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the above code, you can set a custom condition to show the exception and customize the exception text as per your requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it! This is how you can throw exception on Magento 2 admin save configuration.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, do not forget to share this post with your Magento 2 friends via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks for reading!  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you may require to&nbsp;throw exception on Magento 2 admin save configuration. Here is the complete solution to do that. Suppose you have developed a&#8230;<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-2496","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2496","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=2496"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2496\/revisions"}],"predecessor-version":[{"id":13809,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2496\/revisions\/13809"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=2496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=2496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=2496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}