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 in Forms

Hi, My regex always return false: "Phalcon\Validation\Validator\Regex"

        $name = new Text('username');
        $name->setLabel('Usename');
        $name->addValidators([
            new PresenceOf(['message' => _('Username is required')]),
            new UniquenessValidator([
                "model"   => new Users(),
                "attribute" => "username",
                "message" => _("This username is already in use"),
            ]),
            new Regex([
                "message"    => "Username can only contain letters and numbers",
                'pattern' => '^[a-zA-Z0-9_.-]*$',
                "allowEmpty" => true
            ]),
        ]);
        $this->add($name);

What's wrong ?

hi @cosmo you have to set a full regex and the result must equal to the element's value. Check the code

new Regex([
    "message"    => "Username can only contain letters and numbers",
    'pattern' => '/^([a-z0-9_.-]*)$/i',
    "allowEmpty" => true
]),

Good lucki