Is it possible to create a custom validation for fields in configuration? (Stores> Settings> configuration)
I created a system.xml file but the default values for the validate tag are not enough for my needs and I would like to create my own rules.
I found an other question about this but it doesn't say how and where I should put/declare the validation method.
Is this possible or is the only solution to create a backend model with a beforeSave function?
-
To your question on how/where put the code for magento.stackexchange.com/questions/139023/… : You have to create a custom js file and then add js code in it. Then include that file in your code.Jaimin Sutariya– Jaimin Sutariya2017年02月01日 16:03:14 +00:00Commented Feb 1, 2017 at 16:03
-
See my updated answer.Khoa Truong– Khoa Truong2017年02月19日 10:57:54 +00:00Commented Feb 19, 2017 at 10:57
1 Answer 1
As far as I know, if we want to add more custom validations. We should validate with beforeSave(). For example, take a look the cookie validation:
vendor/magento/module-cookie/Model/Config/Backend/Lifetime.php.
We can add more Js validation for the user interface input.
A Sample to add Js validation for system xml:
In your module, create file the layout - adminhtml_system_config_edit.xml
app/code/Vendor/Module/view/adminhtml/layout/adminhtml_system_config_edit.xml
<?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>
<referenceContainer name="js">
<block class="Magento\Backend\Block\Template"
template="Module_Vendor::system/config/custom_js.phtml"/>
</referenceContainer>
</body>
</page>
Remember to add your custom validation in the template.
-
Thanks for the help. I used the beforesave function in a backend model to validate my fields. But your solution seemsThomas F– Thomas F2017年02月20日 09:08:14 +00:00Commented Feb 20, 2017 at 9:08
Explore related questions
See similar questions with these tags.