为什么显示错误而不显示自定义消息?

时间:2018-07-31 00:50:11

标签: php laravel

我有下面的代码,但是自定义消息没有出现,而是在提交表单时出现了一个错误:

  

类型错误:传递给App \ Http \ Controllers \ Controller :: validate()的参数1必须是Illuminate \ Http \ Request的实例

您知道可能是什么问题吗?

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));
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您可以修改send()函数的以下行:

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

对此:

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

因为,validate()的第一个参数采用Request实例,第二个参数采用“验证”规则,而第三个参数则采用自定义消息。

因此,您的participantRules应该重新写为波纹管:

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');
}

您正在尝试在此处验证,而没有任何$ request实例,这不是正确的方法。要解决此问题,您只需返回所需的必需参数即可。