为我们的Magento 2开发人员撰写的另一篇简短而甜蜜的文章,旨在了解如何在Magento 2中提供所有验证以添加到adminhtml system.xml-中的系统配置中:
| 'validate-no-html-tags' | => | “验证不允许的HTML标签” | 
| “验证选择” | => | “是否选择了验证选项” | 
| “必填项” | => | “验证必填字段” | 
| “验证号” | => | “在字段中验证有效数字” | 
| “验证数字” | => | “仅验证字段中的数字,例如无点或无逗号” | 
| “验证日期” | => | “确认有效日期” | 
| '验证电子邮件' | => | '验证有效的电子邮件地址。例如johndoe@domain.com' | 
| 验证网址 | => | '验证有效的网址。协议是必需的(http://,https://或ftp://)' | 
| “验证非负数” | => | “验证字段中没有负数” | 
| “验证零或更大” | => | “在此字段中验证数字应为0或更大” | 
| “验证状态” | => | “应选择验证有效的州/省” | 
| 'validate-cc-number' | => | “验证有效的信用卡号” | 
| 验证数据 | => | “验证此字段应在此字段中仅包含字母(az或AZ),数字(0-9)或下划线(_)” | 
| “验证无空” | => | “验证字段不应该为空” | 
| '验证长度' | => | “验证字段的长度” | 
| “最小长度为10” | => | “验证字段的最小长度” | 
| '最大长度250' | => | “验证字段的最大长度” | 
快速示例展示如何在system.xml配置的Magento 2中使用此方法
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <tab id="scommerce" translate="label" sortOrder="200">
            <label>Scommerce Configuration</label>
        </tab>
        <section id="deliveryinstruction" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Delivery Instruction</label>
            <tab>scommerce</tab>
            <resource>Scommerce_DeliveryInstruction::configuration</resource>
            <group id="configuration" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Configuration</label>
                <field id="label" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
                    <label>Delivery Instruction Label</label>
                    <comment>Delivery instruction label</comment>
                    <validate>required-entry validate-length minimum-length-10 maximum-length-32</validate>
                    <depends>
                        <field id="deliveryinstruction/general/enable">1</field>
                    </depends>
                </field>
 注意:使用上述代码,将根据必填数据验证交付说明标签,并且字段长度至少应为10个字符,但不得超过32个字符。
就是这样,希望本文能以某种方式对您有所帮助。请留下您的评论,让我们知道您的想法?谢谢。
