yii2yii2-modelyii2-validation

Yii2 model rules match validation


In model rules I have this rule:

[
  ['field1', 'field2,]
  'match',
  'pattern' => "/^[а-яА-ЯіІїЇєЄa-zA-Z\s\`\'\-\,]*$/u",
]

On form submit this rule works fine. But on client side it's not working. In console I see error:

Uncaught SyntaxError: Invalid regular expression: /^[а-яА-ЯіІїЇєЄa-zA-Z\s\`'-,]*$/: Invalid escape

Why this rule doesn't works on client side?


Solution

  • Escape with double slash fixes client side without crashing server side

    [
      ['field1', 'field2,]
      'match',
      'pattern' => "/^[а-яА-ЯіІїЇєЄa-zA-Z\s\\\`\\\'-\\\,]*$/u",
    ]