I seems like I was wrong about the validation being on the user content. PostValidator
actually validates the actual database column value, because Flarum calls the validator with $post->getAttributes()
as input rather than $post->content
, and due to how the post model is implemented, ->getAttributes()
gets the raw database value while only ->content
is unparsed.
So basically, this works, but validates the TextFormatter XML:
(new Extend\Validator(\Flarum\Post\PostValidator::class))
->configure(function ($flarumValidator, $validator) {
$rules = $validator->getRules();
// Not all attributes are validated every time. Skip if content is not being validated
if (!array_key_exists('content', $rules)) {
return;
}
// Add new rule to existing array of rules
$rules['content'][] = 'min:150';
$validator->setRules($rules);
}),
And this code could be used to validate the actual post content:
(new Extend\Event)
->listen(\Flarum\Post\Event\Saving::class, function ($event) {
if ($event->post->isDirty('content')) {
resolve(\Illuminate\Contracts\Validation\Factory::class)->make([
'content' => $event->post->content,
], [
'content' => 'min:15',
])->validate();
}
})