In the example for the username validation the existing regex must be replaced because we are adding a rule that's less constrained than the original. But in this situation since we want to add additional constraints that don't conflict with the existing constraints, we can just append a new validation rule.
The Laravel validator also can accept multiple regex:
validation rules so there's no need to loop through the existing rules, we can just append a new one at the end.
And finally, don't forget to escape dots 😉
In my example I have shortened the regex for the situation where we only want to check the end of the string and not validate the name before @
. i
modifier can be removed if we only validate the end of the string and want the domain lowercase.
<?php
use Flarum\Extend;
use Flarum\User\UserValidator;
return [
// Register extenders here
(new Extend\Validator(UserValidator::class))
->configure(function ($flarumValidator, $validator) {
$rules = $validator->getRules();
if (!array_key_exists('email', $rules)) {
return;
}
$rules['email'][] = 'regex:/@example\.com$/';
$validator->setRules($rules);
}),
];
This seems to work on my local install. Of course some additional explanation somewhere of the email syntax would be useful otherwise the user just sees "The email format is invalid" without any more details. There's a way to customize the validation error message but I'd need to go dig in the source code to remember how that can be changed.