I have a custom admin form and unfortunately it doesnt have a UI component xml file.
I was able to add the no-whitespace class validation.
I would add also a regexp pattern, but I don't know where I should specify the format.
I tried
'class' => 'no-whitespace pattern',
'pattern' => '^[0-9]+(,[0-9]+)*$'
but only no-whitespace works fine
any idea? tnx
1 Answer 1
Antonio Pedicini, instead of add custom pattern direct to input field ,my suggest to create a magento 2 create custom validation
So, extend rules.js to add custom validation rules for this pattern.
First create folder inside at app/design/adminhtml/Magento/backend/Magento_Ui/web/js/lib/validation/rules.js if its already exist then skip it keep file into below location from core module.
And that files add
"MyCustompattern": [
function(value) {
return value.match(/^[0-9]+(,[0-9]+)*$/);
},
$.mage.__('My Custom Validaion')
],
after
"no-whitespace": [
function(value) {
return /^\S+$/i.test(value);
},
$.mage.__('No white space please')
],
Then add MyCustompattern in 'class' => 'no-whitespace MyCustompattern'
Final delete all file from var/view_preprocessed and var/cache,var/page_cache. Removepub/static `folder contents and run deploy command:
for see ,it is working or not.
php bin/magento setup:static-content:deploy
Or,you can follow below blog for create custom rules
http://inchoo.net/magento-2/validate-custom-form-in-magento-2/
http://www.webmull.com/magento-2-add-new-custom-form-validation/
-
tnx Amit, but in that case the rule will be site wide? I just need it on 1 admin form, why do you think is not a good idea to use pattern on the input? Also, do you have any example how to use it? i'm struggling to find anyapedic– apedic2017年09月27日 09:30:15 +00:00Commented Sep 27, 2017 at 9:30
-
Amit, would that solution extend or override the core rule.js so I need to check against any update?apedic– apedic2017年09月28日 10:27:16 +00:00Commented Sep 28, 2017 at 10:27