Magento2 : Form Validation Rule

Validation Using Ui-Component :
<field name="email" sortOrder="4" formElement="input">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="source" xsi:type="string">block</item>
                </item>
            </argument>
            <settings>
                <validation>
                    <rule name="required-entry" xsi:type="boolean">true</rule>
                    <rule name="validate-emails" xsi:type="boolean">true</rule>
                </validation>
                <dataType>text</dataType>
                <label translate="true">Email</label>
                <dataScope>email</dataScope>
            </settings>
        </field>

Form Validation using phtml file
 <input class="admin__control-text" name="sale_name" id="sale_name" type="text" data-validate='{"required":true,"validate-emails":true}' >
The validators list in UI Components it can be found in 
  vendor/magento/module-ui/view/base/web/js/lib/validation/rules.js

Just wrote this script to grab all keys with error messages for explanation:

required-entry:
   This is a required field.
validate-alphanum-with-spaces:
   Please use only letters (a-z or A-Z), numbers (0-9) or spaces only in this field.
phoneUK:
   Please specify a valid phone number
validate-email:
   Please enter a valid email address (Ex: johndoe@domain.com).
ipv4:
   Please enter a valid IP v4 address.
checked:
   This is a required field.
validate-select:
   Please select an option.
ipv6:
   Please enter a valid IP v6 address.
time:
   Please enter a valid time, between 00:00 and 23:59
validate-number:
   Please enter a valid number in this field.
validate-phoneLax:
   Please enter a valid phone number. For example (123) 456-7890 or 123-456-7890.
dateITA:
   Please enter a correct date
validate-xml-identifier:
   Please enter a valid XML-identifier (Ex: something_1, block5, id-4).
validate-clean-url:
   Please enter a valid URL. For example http://www.example.com or www.example.com.
validate-admin-password:
   Please enter 7 or more characters, using both numeric and alphabetic.
validate-no-html-tags:
   HTML tags are not allowed.
validate-integer:
  Please enter a valid integer in this field.
validate-data:
  Please use only letters (a-z or A-Z), numbers (0-9) or underscore (_) in this field, and the first character should be a letter.
validate-cc-ukss:
  Please enter issue number or start date for switch/solo card type.
validate-street:
  Please use only letters (a-z or A-Z), numbers (0-9), spaces and "#" in this field.
validate-zip-international:
  Please enter a valid zip code.
validate-date:
  Please enter a valid date.
validate-greater-than-zero:
  Please enter a number greater than 0 in this field.
validate-digits:
  Please enter a valid number in this field.
validate-ssn:
  Please enter a valid social security number (Ex: 123-45-6789).
not-negative-amount:
  Please enter positive number in this field.
validate-max-size:
  File you are trying to upload exceeds maximum file size limit.
validate-fax:
  Please enter a valid fax number (Ex: 123-456-7890).
validate-if-tag-script-exist:
  Please use tag SCRIPT with SRC attribute or with proper content to include JavaScript to the document.
validate-date-au:
  Please use this date format: dd/mm/yyyy. For example 17/03/2006 for the 17th of March, 2006.
mobileUK:
  Please specify a valid mobile number
letters-with-basic-punc:
  Letters or punctuation only please
validate-number-range:
  The value is not within the specified range.
phoneUS:
  Please specify a valid phone number
date_range_max:
  The date is not within the specified range.
validate-range:
  The value is not within the specified range.
validate-state:
  Please select State/Province.
validate-url:
  Please enter a valid URL. Protocol is required (http://, https:// or ftp://).
validate-currency-dollar:
  Please enter a valid $ amount. For example $100.00.
time12h:
  Please enter a valid time, between 00:00 am and 12:00 pm

Comments

Post a Comment