为什么自定义消息没有出现?

时间:2018-07-02 16:54:36

标签: php laravel

我有此代码可以发送电子邮件。主题和消息字段是必填字段。但是,如果用户未填写此字段并单击“发送”,则会显示:

The subject field is required.
The message field is required.

代替出现自定义消息:

The field subject is mandatory to send email.
The field message is mandatory to send email.

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

public function contactOrganizer($id, Request $request){
        $conference = Conference::find($id);


        $rules = [
            'subject' => 'required',
            'message' => 'required',
        ];


        $customMessages = [
            'subject' => 'The field subject is mandatory to send email.',
            'message' => 'The field message is mandatory to send email.',
        ];


        $this->validate($request, $rules, $customMessages);

        $message = $request->message;
        $subject = $request->subject;

        Mail::to($conference->organizer_email)
        ->send(new UserNotification
        ($conference, $message, $subject));
    }

$ rules显示:

array:2 [▼
  "subject" => "required"
  "message" => "required"
]

$ customMessages显示:

array:2 [▼
  "subject" => "The field subject is mandatory to send email."
  "message" => "The field message is mandatory to send email."
]

1 个答案:

答案 0 :(得分:1)

是,将代码更改为此。您需要在字段名称中添加.required。或您尝试显示其自定义消息的任何属性。

$customMessages = [
            'subject.required' => 'The field subject is mandatory to send email.',
            'message.required' => 'The field message is mandatory to send email.',
        ];