1

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?

enter image description here

I'm using Magento 2.1.2 with vagrant.

5
  • Try 'pattern' => '^[0-9]$'
    – Jackson
    Commented Feb 16, 2017 at 3:23
  • Hi Ankit, thanks for you help. Unfortunately this doesn't work, it generates the same blank checkout page (see screenshot above). Any other suggestions?
    – Tjitse
    Commented Feb 16, 2017 at 8:12
  • Check your browser console for javascript errors. There might be something useful there. What you are changing is basically a configuration for the JS on the checkout page. If rendering stops it might be that something breaks the javascript execution.
    – nenne
    Commented Feb 16, 2017 at 8:31
  • I am having the same issue. The reason is that the pattern function expects a JS regular expression and it is receiving a string instead.
    – graney
    Commented Feb 17, 2017 at 10:31
  • And did you find the solution @graney?
    – Tjitse
    Commented Feb 20, 2017 at 13:37

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.