Here's the deal:
Validator:
namespace Xelson\Chat;
use Flarum\Foundation\AbstractValidator;
use Flarum\Settings\SettingsRepositoryInterface;
class MessageValidator extends AbstractValidator
{
/**
* @var SettingsRepositoryInterface
*/
protected $settings;
/**
* @param SettingsRepositoryInterface $settings
*/
public function __construct(SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
}
protected function getRules()
{
$max_chars = $this->settings->get('pushedx-chat.charlimit');
return
[
'message' =>
[
'required',
"max:$max_chars"
]
];
}
}
Command that which should be validated:
<?php
/*
* This file is part of xelson/flarum-ext-chat
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xelson\Chat\Commands;
use Carbon\Carbon;
use Xelson\Chat\Message;
use Xelson\Chat\PusherWrapper;
use Xelson\Chat\MessageValidator;
use Flarum\User\AssertPermissionTrait;
use Flarum\Foundation\DispatchEventsTrait;
use Flarum\Foundation\Application;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Events\Dispatcher;
class PostChatHandler
{
use DispatchEventsTrait;
use AssertPermissionTrait;
/**
* @var Application
*/
protected $app;
/**
* @var SettingsRepositoryInterface
*/
protected $settings;
/**
* @var PusherWrapper
*/
protected $pusher;
/**
* @var MessageValidator
*/
protected $validator;
/**
* @param Dispatcher $events
* @param Application $app
* @param SettingsRepositoryInterface $settings
* @param PusherWrapper $pusher
*/
public function __construct(
Dispatcher $events,
Application $app,
SettingsRepositoryInterface $settings,
PusherWrapper $pusher,
MessageValidator $validator
) {
$this->events = $events;
$this->app = $app;
$this->settings = $settings;
$this->pusher = $pusher->pusher;
$this->validator = $validator;
}
/**
* Handles the command execution.
*
* @param PostChat $command
* @return null|string
*/
public function handle(PostChat $command)
{
$actor = $command->actor;
$content = $command->msg;
$this->assertCan(
$actor,
'pushedx-chat.permissions.chat'
);
$message = Message::build(
$content,
$actor->id,
Carbon::now()
);
$this->validator->assertValid($message->getDirty()); // <- breakpoint here
$message->save();
$msg = [
'id' => $message->id,
'actorId' => $actor->id,
'message' => $content
];
$this->pusher->trigger('public', 'eventPost', $msg);
return $message;
}
}
Everything seems to be correct, but I get an exception:
POST http://forum.neonsky.test/api/chat/17
{
"errors": [
{
"status": "500",
"code": "unknown",
"detail": "Error: Call to a member function make() on null in C:\\laragon\\www\\forum.neonsky\\vendor\\flarum\\core\\src\\Foundation\\AbstractValidator.php:93\nStack trace:\n#0 C:\\laragon\\www\\forum.neonsky\\vendor\\flarum\\core\\src\\Foundation\\AbstractValidator.php(60): Flarum\\Foundation\\AbstractValidator->makeValidator(Array)\n#1 C:\\laragon\\www\\forum.neonsky\\vendor\\workbench\\flarum-ext-chat\\src\\Commands\\EditChatHandler.php(98): Flarum\\Foundation\\AbstractValidator->assertValid(Array)\n#2 C:\\laragon\\www\\forum.neonsky\\vendor\\illuminate\\bus\\Dispatcher.php(90): Xelson\\Chat\\Commands\\EditChatHandler->handle(Object(Xelson\\Chat\\Commands\\EditChat))\n#3 C:\\laragon\\www\\forum.neonsky\\vendor\\illuminate\\pipeline\\Pipeline.php(128): Illuminate\\Bus\\Dispatcher->Illuminate\\Bus\\{closure}(Object(Xelson\\Chat\\Commands\\EditChat))\n#4 C:\\laragon\\www\\forum.neonsky\\vendor\\illuminate\\pipeline\\Pipeline.php(104): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Xelson\\Chat\\Commands\\EditChat))\n#5 C:\\laragon\\www\\forum.neonsky\\vendor\\illuminate\\bus\\Dispatcher.php(98): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))\n#6 C:\\laragon\\www\\forum.neonsky\\vendor\\illuminate\\bus\\Dispatcher.php(76): Illuminate\\Bus\\Dispatcher->dispatchNow(Object(Xelson\\Chat\\Commands\\EditChat))\n#7 C:\\laragon\\www\\forum.neonsky\\vendor\\workbench\\flarum-ext-chat\\src\\Api\\Controllers\\EditChatController.php(56): Illuminate\\Bus\\Dispatcher->dispatch(Object(Xelson\\Chat\\Commands\\EditChat))\n#8 C:\\laragon\\www\\forum.neonsky\\vendor\\flarum\\core\\src\\Api\\Controller\\AbstractSerializeController.php(96): Xelson\\Chat\\Api\\Controllers\\EditChatController->data(Object(Zend\\Diactoros\\ServerRequest), Object(Tobscure\\JsonApi\\Document))\n#9 C:\\laragon\\www\\forum.neonsky\\vendor\\flarum\\core\\src\\Http\\RouteHandlerFactory.php(38): Flarum\\Api\\Controller\\AbstractSerializeController->handle(Object(Zend\\Diactoros\\ServerRequest))\n#10 C:\\laragon\\www\\forum.neonsky\\vendor\\flarum\\core\\src\\Http\\Middleware\\DispatchRoute.php(65): Flarum\\Http\\RouteHandlerFactory->Flarum\\Http\\{closure}(Object(Zend\\Diactoros\\ServerRequest), Array)\n#11 C:\\laragon\\www\\forum.neonsky\\vendor\\zendframework\\zend-stratigility\\src\\Next.php(60): Flarum\\Http\\Middleware\\DispatchRoute->process(Object(Zend\\Diactoros\\ServerRequest), Object(Closure))\n#12 C:\\laragon\\www\\forum.neonsky\\vendor\\flarum\\core\\src\\Http\\Middleware\\SetLocale.php(50): Zend\\Stratigility\\Next->handle(Object(Zend\\Diactoros\\ServerRequest))\n#13 C:\\laragon\\www\\forum.neonsky\\vendor\\zendframework\\zend-stratigility\\src\\Next.php(60): Flarum\\Http\\Middleware\\SetLocale->process(Object(Zend\\Diactoros\\ServerRequest), Object(Zend\\Stratigility\\Next))\n#14 C:\\laragon\\www\\forum.neonsky\\vendor\\flarum\\core\\src\\Http\\Middleware\\CheckCsrfToken.php(31): Zend\\Stratigility\\Next->handle(Object(Zend\\Diactoros\\ServerRequest))\n#15 C:\\laragon\\www\\forum.neonsky\\vendor\\zendframework\\zend-stratigility\\src\\Next.php(60): Flarum\\Http\\Middleware\\CheckCsrfToken->process(Object(Zend\\Diactoros\\ServerRequest), Object(Zend\\Stratigility\\Next))\n#16 C:\\laragon\\www\\forum.neonsky\\vendor\\flarum\\core\\src\\Http\\Middleware\\AuthenticateWithHeader.php(55): Zend\\Stratigility\\Next->handle(Object(Zend\\Diactoros\\ServerRequest))\n#17 C:\\laragon\\www\\forum.neonsky\\vendor\\zendframework\\zend-stratigility\\src\\Next.php(60): Flarum\\Http\\Middleware\\AuthenticateWithHeader->process(Object(Zend\\Diactoros\\ServerRequest), Object(Zend\\Stratigility\\Next))\n#18 C:\\laragon\\www\\forum.neonsky\\vendor\\flarum\\core\\src\\Http\\Middleware\\AuthenticateWithSession.php(32): Zend\\Stratigility\\Next->handle(Object(Zend\\Diactoros\\ServerRequest))\n#19 C:\\laragon\\www\\forum.neonsky\\vendor\\zendframework\\zend-stratigility\\src\\Next.php(60): Flarum\\Http\\Middleware\\AuthenticateWithSession->process(Object(Zend\\Diactoros\\ServerRequest), Object(Zend\\Stratigility\\Next))\n#20 C:\\laragon\\www\\forum.neonsky\\vendor\\flarum\\core\\src\\Http\\Middleware\\RememberFromCookie.php(51): Zend\\Stratigility\\Next->handle(Object(Zend\\Diactoros\\ServerRequest))\n#21 C:\\laragon\\www\\forum.neonsky\\vendor\\zendframework\\zend-stratigility\\src\\Next.php(60): Flarum\\Http\\Middleware\\RememberFromCookie->process(Object(Zend\\Diactoros\\ServerRequest), Object(Zend\\Stratigility\\Next))\n#22 C:\\laragon\\www\\forum.neonsky\\vendor\\flarum\\core\\src\\Http\\Middleware\\StartSession.php(61): Zend\\Stratigility\\Next->handle(Object(Zend\\Diactoros\\ServerRequest))\n#23 C:\\laragon\\www\\forum.neonsky\\vendor\\zendframework\\zend-stratigility\\src\\Next.php(60): Flarum\\Http\\Middleware\\StartSession->process(Object(Zend\\Diactoros\\ServerRequest), Object(Zend\\Stratigility\\Next))\n#24 C:\\laragon\\www\\forum.neonsky\\vendor\\flarum\\core\\src\\Api\\Middleware\\FakeHttpMethods.php(29): Zend\\Stratigility\\Next->handle(Object(Zend\\Diactoros\\ServerRequest))\n#25 C:\\laragon\\www\\forum.neonsky\\vendor\\zendframework\\zend-stratigility\\src\\Next.php(60): Flarum\\Api\\Middleware\\FakeHttpMethods->process(Object(Zend\\Diactoros\\ServerRequest), Object(Zend\\Stratigility\\Next))\n#26 C:\\laragon\\www\\forum.neonsky\\vendor\\flarum\\core\\src\\Http\\Middleware\\ParseJsonBody.php(28): Zend\\Stratigility\\Next->handle(Object(Zend\\Diactoros\\ServerRequest))\n#27 C:\\laragon\\www\\forum.neonsky\\vendor\\zendframework\\zend-stratigility\\src\\Next.php(60): Flarum\\Http\\Middleware\\ParseJsonBody->process(Object(Zend\\Diactoros\\ServerRequest), Object(Zend\\Stratigility\\Next))\n#28 C:\\laragon\\www\\forum.neonsky\\vendor\\flarum\\core\\src\\Http\\Middleware\\HandleErrors.php(57): Zend\\Stratigility\\Next->handle(Object(Zend\\Diactoros\\ServerRequest))\n#29 C:\\laragon\\www\\forum.neonsky\\vendor\\zendframework\\zend-stratigility\\src\\Next.php(60): Flarum\\Http\\Middleware\\HandleErrors->process(Object(Zend\\Diactoros\\ServerRequest), Object(Zend\\Stratigility\\Next))\n#30 C:\\laragon\\www\\forum.neonsky\\vendor\\zendframework\\zend-stratigility\\src\\MiddlewarePipe.php(83): Zend\\Stratigility\\Next->handle(Object(Zend\\Diactoros\\ServerRequest))\n#31 C:\\laragon\\www\\forum.neonsky\\vendor\\middlewares\\request-handler\\src\\RequestHandler.php(84): Zend\\Stratigility\\MiddlewarePipe->process(Object(Zend\\Diactoros\\ServerRequest), Object(Zend\\Stratigility\\Next))\n#32 C:\\laragon\\www\\forum.neonsky\\vendor\\zendframework\\zend-stratigility\\src\\Next.php(60): Middlewares\\RequestHandler->process(Object(Zend\\Diactoros\\ServerRequest), Object(Zend\\Stratigility\\Next))\n#33 C:\\laragon\\www\\forum.neonsky\\vendor\\middlewares\\base-path-router\\src\\BasePathRouter.php(97): Zend\\Stratigility\\Next->handle(Object(Zend\\Diactoros\\ServerRequest))\n#34 C:\\laragon\\www\\forum.neonsky\\vendor\\zendframework\\zend-stratigility\\src\\Next.php(60): Middlewares\\BasePathRouter->process(Object(Zend\\Diactoros\\ServerRequest), Object(Zend\\Stratigility\\Next))\n#35 C:\\laragon\\www\\forum.neonsky\\vendor\\zendframework\\zend-stratigility\\src\\Middleware\\OriginalMessages.php(41): Zend\\Stratigility\\Next->handle(Object(Zend\\Diactoros\\ServerRequest))\n#36 C:\\laragon\\www\\forum.neonsky\\vendor\\zendframework\\zend-stratigility\\src\\Next.php(60): Zend\\Stratigility\\Middleware\\OriginalMessages->process(Object(Zend\\Diactoros\\ServerRequest), Object(Zend\\Stratigility\\Next))\n#37 C:\\laragon\\www\\forum.neonsky\\vendor\\middlewares\\base-path\\src\\BasePath.php(53): Zend\\Stratigility\\Next->handle(Object(Zend\\Diactoros\\ServerRequest))\n#38 C:\\laragon\\www\\forum.neonsky\\vendor\\zendframework\\zend-stratigility\\src\\Next.php(60): Middlewares\\BasePath->process(Object(Zend\\Diactoros\\ServerRequest), Object(Zend\\Stratigility\\Next))\n#39 C:\\laragon\\www\\forum.neonsky\\vendor\\zendframework\\zend-stratigility\\src\\MiddlewarePipe.php(83): Zend\\Stratigility\\Next->handle(Object(Zend\\Diactoros\\ServerRequest))\n#40 C:\\laragon\\www\\forum.neonsky\\vendor\\zendframework\\zend-stratigility\\src\\MiddlewarePipe.php(72): Zend\\Stratigility\\MiddlewarePipe->process(Object(Zend\\Diactoros\\ServerRequest), Object(Zend\\Stratigility\\EmptyPipelineHandler))\n#41 C:\\laragon\\www\\forum.neonsky\\vendor\\zendframework\\zend-httphandlerrunner\\src\\RequestHandlerRunner.php(95): Zend\\Stratigility\\MiddlewarePipe->handle(Object(Zend\\Diactoros\\ServerRequest))\n#42 C:\\laragon\\www\\forum.neonsky\\vendor\\flarum\\core\\src\\Http\\Server.php(44): Zend\\HttpHandlerRunner\\RequestHandlerRunner->run()\n#43 C:\\laragon\\www\\forum.neonsky\\public\\index.php(22): Flarum\\Http\\Server->listen()\n#44 {main}"
}
]
}
Well, what am I doing wrong?