I'm using a plugin to validate the street field (based on https://magento.stackexchange.com/a/158101/28803) and I want to use the validation method 'pattern' to test for the following regex:
.*[0-9].*
Complete code:
```
public function afterProcess(
\Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
array $jsLayout
) {
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children']['street'] = [
'component' => 'Magento_Ui/js/form/components/group',
'label' => __('Street Address'),
'required' => true,
'dataScope' => 'shippingAddress.street',
'provider' => 'checkoutProvider',
'sortOrder' => 60,
'type' => 'group',
'additionalClasses' => 'street',
'children' => [
[
'component' => 'Magento_Ui/js/form/element/abstract',
'config' => [
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'ui/form/element/input'
],
'dataScope' => '0',
'provider' => 'checkoutProvider',
'validation' => ['required-entry' => true, 'pattern' => '.*[0-9].*'],
],
[
'component' => 'Magento_Ui/js/form/element/abstract',
'config' => [
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'ui/form/element/input'
],
'dataScope' => '1',
'provider' => 'checkoutProvider',
'validation' => ['required-entry' => true, 'pattern' => ".*[0-9].*"],
]
]
];
return $jsLayout;
}
} ```
However validation' => ['required-entry' => true, 'pattern' => ".*[0-9].*"], doesn't work (it generates a blank page on checkout). What am I doing wrong?
I'm using Magento 2.1.2 with vagrant.
1 Answer 1
I've found a different method; by using a custom validation method in rules.js:
"validate-adres": [
function(value) {
return /[0-9]/.test(value);
},
$.mage.__('Your street address must contain a housenumber')
],
Combined with the above answer snippet this validates the street address with regex.
'pattern' => '^[0-9]$'