Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Chris1904's avatar

must be an instance of Facades\Validator, instance of Validation\Validator given

Hi everyone,

When I try to submit a new discussion I receive this error:

FatalThrowableError in ChatterBeforeNewDiscussion.php line 26:
Type error: Argument 2 passed to DevDojo\Chatter\Events\ChatterBeforeNewDiscussion::__construct() must be an instance of Illuminate\Support\Facades\Validator, instance of Illuminate\Validation\Validator given, called in /var/www/vhosts/domain/httpdocs/vendor/devdojo/chatter/src/Controllers/ChatterDiscussionController.php on line 66

ChatterBeforeNewDiscussion.php line 26

use Validator;
...

public function __construct(Request $request, Validator $validator)
{
    // no error message if I comment out the following lines
    $this->request = $request;
    $this->validator = $validator;
}

ChatterDiscussionController.php on line 66

use Validator;
...

$validator = Validator::make($request->all(), [
    'title'               => 'required|min:5|max:255',
    'body_content'        => 'required|min:10',
    'chatter_category_id' => 'required',
]);

Event::fire(new ChatterBeforeNewDiscussion($request, $validator));

Am I doing anything wrong here? The second argument seems to be an instance of Illuminate\Support\Facades\Validator so I don't really understand this error message. :-(

Any help is appreciated!

0 likes
4 replies
jlrdw's avatar

Do you have all the other "use" statements like

<?php

namespace App\Http\Controllers;

use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
Chris1904's avatar

ChatterDiscussionController.php

namespace DevDojo\Chatter\Controllers;

use Auth;
use Carbon\Carbon;
use DevDojo\Chatter\Events\ChatterAfterNewDiscussion;
use DevDojo\Chatter\Events\ChatterBeforeNewDiscussion;
use DevDojo\Chatter\Models\Models;
use Event;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as Controller;
use Validator;

and ChatterBeforeNewDiscussion.php

namespace DevDojo\Chatter\Events;

use Illuminate\Http\Request;
use Validator;

Thanks for your willingness to help out.

jlrdw's avatar

Is this part even needed?

public function __construct(Request $request, Validator $validator)
{
    // no error message if I comment out the following lines
    $this->request = $request;
    $this->validator = $validator;
}

I can't find an example of usage in a constructor.

clay's avatar

You shouldn't be passing a facade to a constructor and assigning it to a property. Facades are designed to be used directly. just

<?php

namespace App\Http\Controllers;

use Validator;

class MyController
{
    public function store
    {
        $validator = Validator::make($request->all(), [
            'title' => 'required|unique:posts|max:255',
            'body' => 'required',
        ]);

        if ($validator->fails()) {
            return redirect('post/create')
                        ->withErrors($validator)
                        ->withInput();
        }
    }
}

or, in your controllers, if you're extending the base controller, you can just use the $validate method that is provided via the ValidatesRequests trait. e.g.

public function store(Request $request)
{
    $this->validate($request, [
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
    ]);

    // Validation passed, store in database...
}

Please or to participate in this conversation.