user10066925
user10066925

Reputation:

Why it shows an error instead of show the custom messages?

I have the code below but there is an issue the custom messages are not appearing, instead it appears an error when the form is submitted:

Type error: Argument 1 passed to App\Http\Controllers\Controller::validate() must be an instance of Illuminate\Http\Request

Do you know what can be the issue?

namespace App\Http\Controllers;
use App\Mail\Notification;
use Illuminate\Http\Request;
use App\Conference;
use App\RegistrationType;
use App\User;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Session;

class NotificationController extends Controller
{
    public function send(Request $request, $id)
    {
        $conference = Conference::find($id);

        $message = $request->message;
        $subject = $request->subject;
        $emails = [];

        $this->validate(request(), $this->participantRules($id));

        if($request->send_to == "participant"){

            $emails = User::whereHas('registrations', function ($query) use($id) {
                $query->where('conference_id', '=', $id);
            })->where('email', $request->email)->pluck('email');
        }else if($request->send_to == "all"){

            $emails = User::whereHas('registrations', function ($query) use($id) {
                $query->where('conference_id', '=', $id);
            })->pluck('email');
        }else{

            $emails = User::whereHas('registrations.participants.registration_type', function ($query) use ($id, $request) {
                $query->where('id', '=', $request->send_to)
                    ->where('conference_id', '=', $id);
            })->whereHas('registrations', function ($query) use ($id) {
                $query->where('conference_id', '=', $id);
            })->pluck('email');

        }

        if(count($emails) > 0) {
            $this->sendNotification($emails, $conference, $request);
            Session::flash('success', 'Notification sent with success.');
            return redirect()->back();
        }else{
            Session::flash('no_participants', 'There are no participants registered in the conference.');
            return redirect()->back();
        }
    }
    protected function participantRules($conferenceID){
        $rules = [
            'email' => 'required_if:send_to,participant|email|nullable',
            'subject' => 'required',
            'message' => 'required'
        ];

        $customMessages = [
            'email.required_if' => 'The field email is mandatory.',
            'email.email' => 'Email has an invalid format.',
            'subject.required' => 'The field subject is required.',
            'message.required' => 'The field message is required.',
        ];
        $this->validate( $rules, $customMessages);
    }
    protected function sendNotification($emails, $conference, $request){
        foreach ($emails as $userEmail) {
            Mail::to($userEmail)->send(new Notification($conference, $request->message, $request->subject));
        }
    }
}

Upvotes: 0

Views: 48

Answers (1)

Imran
Imran

Reputation: 4750

You can modify the following line of send() function:

$this->validate(request(), $this->participantRules($id));

To this:

$rules = $this->participantRules($id);
$this->validate($request, $rules['rules'], $rules['customMessages']);

Because, the first parameter of validate() takes the Request instance, second parameter takes Validation rules and 3rd parameters take the custom message.

So your participantRules should be re-written as bellow:

function participantRules($conferenceID)
{
    $rules = [
        'email' => 'required_if:send_to,participant|email|nullable',
        'subject' => 'required',
        'message' => 'required'
    ];

    $customMessages = [
        'email.required_if' => 'The field email is mandatory.',
        'email.email' => 'Email has an invalid format.',
        'subject.required' => 'The field subject is required.',
        'message.required' => 'The field message is required.',
    ];
    return compact('rules', 'customMessages');
}

You are trying to validate here without any $request instance which is not the correct way. To fix this you can just return the required parameters you need.

Upvotes: 2

Related Questions