介绍了如何在Magento 2中使用所有验证以添加到adminhtml system.xml-中的系统配置中:
'validate-no-html-tags' | => | “验证不允许的HTML标签” |
‘validate-select’ | => | “是否选择了验证选项” |
‘required-entry’ | => | “验证必填字段” |
‘validate-number’ | => | “在字段中验证有效数字” |
‘validate-digits’ | => | “仅验证字段中的数字,例如无点或无逗号” |
‘validate-date’ | => | “确认有效日期” |
‘validate-email’ | => | '验证有效的电子邮件地址。例如johndoe@domain.com' |
‘validate-url’ | => | '验证有效的网址。协议是必需的(http://,https://或ftp://)' |
‘validate-not-negative-number’ | => | “验证字段中没有负数” |
‘validate-zero-or-greater’ | => | “在此字段中验证数字应为0或更大” |
‘validate-state’ | => | “应选择验证有效的州/省” |
'validate-cc-number' | => | “验证有效的信用卡号” |
‘validate-data’ | => | “验证此字段应在此字段中仅包含字母(az或AZ),数字(0-9)或下划线(_)” |
‘validate-no-empty’ | => | “验证字段不应为空” |
‘validate-length’ | => | “验证字段的长度” |
‘minimum-length-10’ | => | “验证字段的最小长度” |
‘maximum-length-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个字符。
希望本文能以某种方式对您有所帮助。请留下您的评论,让我们知道您的想法?谢谢。