Last active
June 26, 2022 18:23
-
-
Save Alexander-Pop/30eae4c0c3f38cb2de7ee768fc1d4422 to your computer and use it in GitHub Desktop.
Magento 2 - Form Custom Validations - Frontend #magento2 #validation #checkout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="checkout" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> | |
<body> | |
<referenceBlock name="checkout.root"> | |
<arguments> | |
<argument name="jsLayout" xsi:type="array"> | |
<item name="components" xsi:type="array"> | |
<item name="checkout" xsi:type="array"> | |
<item name="steps" xsi:type="array"> | |
<item name="children" xsi:type="array"> | |
<item name="shipping-step" xsi:type="array"> | |
<item name="children" xsi:type="array"> | |
<item name="shippingAddress" xsi:type="array"> | |
<item name="children" xsi:type="array"> | |
<item name="shipping-address-fieldset" xsi:type="array"> | |
<item name="children" xsi:type="array"> | |
<item name="city" xsi:type="array"> | |
<item name="config" xsi:type="array"> | |
<item name="validation" xsi:type="array"> | |
<item name="validate-alpha-with-spaces" xsi:type="boolean">true</item> | |
</item> | |
</item> | |
</item> | |
</item> | |
</item> | |
</item> | |
</item> | |
</item> | |
</item> | |
</item> | |
</item> | |
</item> | |
</item> | |
</argument> | |
</arguments> | |
</referenceBlock> | |
</body> | |
</page> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var rules = { | |
'validate-min-letters':[ | |
function (value, element, params) { | |
return this.optional(element) || /^[a-zA-Z \-]+$/i.test(value) && value.length >= params; | |
}, | |
'Please enter at least {0} letter without numeric character.' //{0} will be replaced by params | |
], | |
/** In case where you need to translate the error message **/ | |
'validate-min-letters':[ | |
function (value, element, params) { | |
this.params = params; | |
return this.optional(element) || /^[a-zA-Z \-]+$/i.test(value) && value.length >= params; | |
}, function() { | |
return $.mage.__('Please enter at least %1 letters').replace('%1' , this.params) | |
} | |
], | |
'validate-min-character':[ | |
function (value, element, params) { | |
this.params = params; | |
return this.optional(element) || /^(.*)+$/i.test(value) && value.length >= params; | |
}, function() { | |
return $.mage.__('Please enter at least %1 characters').replace('%1' , this.params); | |
} | |
], | |
'validate-zip-fr':[ | |
function(v){ | |
return $.mage.isEmptyNoTrim(v) || /(^\d{5}$)/.test(v); | |
}, | |
$.mage.__('Please enter a valid fr zip code.') | |
], | |
'validate-alpha-with-spaces:[ | |
function (v) { | |
return $.mage.isEmptyNoTrim(v) || /^[a-zA-Z ]+$/.test(v); | |
}, | |
$.mage.__('Please use only letters (a-z or A-Z) or spaces only in this field.') | |
], | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form action="<?php $block->getFormAction();?>"> | |
<!-- Here we want at least 2 letter --> | |
<input name="lastname" id="lastname" title="<?php echo __('Name') ?>" class="input-text" type="text" data-validate="{'validate-min-letters':2, required:true}"/> | |
<!-- Here we want at 5 numeric character --> | |
<input type="text" name="postcode" title="<?php echo __('Zip/Postal Code') ?>" id="zip" class="input-text validate-zip-fr"/> | |
<button type="submit" title="<?php echo __('Submit') ?>"> | |
<span><?php echo __('Submit') ?></span> | |
</button> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Vendor/Module/view/frontend/web/js/validation-mixin.js */ | |
define([ | |
'jquery', | |
'Magento_Ui/js/lib/validation/validator' | |
], function ($, validator) { | |
'use strict'; | |
return function () { | |
$.validator.addMethod( | |
'validate-alpha-with-spaces', | |
function (v) { | |
return $.mage.isEmptyNoTrim(v) || /^[a-zA-Z ]+$/.test(v); | |
}, | |
$.mage.__('Please use only letters (a-z or A-Z) or spaces only in this field.') | |
); | |
/** validator addRule method allow to use our custom validation with ui component field (checkout) */ | |
validator.addRule( | |
'validate-alpha-with-spaces', | |
function (v) { | |
return $.mage.isEmptyNoTrim(v) || /^[a-zA-Z ]+$/.test(v); | |
}, | |
$.mage.__('Please use only letters (a-z or A-Z) or spaces only in this field.') | |
); | |
}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Vendor/Module/view/frontend/requirejs-config.js */ | |
var config = { | |
config: { | |
mixins: { | |
'mage/validation': { | |
'Vendor_Module/js/my-validation-mixin': true, | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment