2
votes

I'm trying to create a custom validation rule as folows:

$validator
    ->add('slug', 'custom', [
        'rule' => function($value, $context) {
            return preg_match('/^[a-z0-9\-]+$/', $value);
        },
        'message' => 'Slug cannot contain spaces or special characters'
    ]);

The rule works fine, but the message on form is always: "The provided value is invalid".

1

1 Answers

2
votes

I just had to cast to boolean the return value, as preg_match() returns 0|1.

return (bool) preg_match('/^[a-z0-9\-]+$/', $value);