We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Regex Validator always return false in Form:(

Hello! Why regex validation doesn't work :(

// Surname
        $surname = new Text('surname', array(
            'placeholder' => 'Не указано'
        ));
        $surname->setDefault($user_info->profile->surname);
        $surname->setLabel('Фамилия');

        $surname->addValidators([new Regex([

                    'message' => 'Фамилия может содержать только символы латиницы или кирилицы',
                    'pattern' => '/^[A-Za-zА-Яа-я]$/u'
                ]),
        ]);

And if I use regex with pattern '/^[0-9]$/' i also get FALSE and error message :(

Try

$surname->addValidators([new Regex([

          'message' => 'Фамилия может содержать только символы латиницы или кирилицы',
          'pattern' => 'pattern' => '/^[A-Za-zА-Яа-я]*$/'
        ]),
    ]);


13.8k
Accepted
answer

I solved this problem by changing regex pattern (added {1,}) to '/^[A-Za-zА-Яа-я]{1,}*$/'