0
我想在我的Laravel應用程序中使用SwiftMailer驗證SMTP服務器設置。Laravel驗證SMTP服務器設置
我的主要問題是,如果SMTP服務器驗證失敗,我希望它以與$this->validate()
相同的方式將用戶返回到帶有錯誤消息的窗體。
這裏是相同的示例代碼,所以你可以看到我想要做的事:
public function update($id)
{
$this->validate(request(), [
"name" => "required|unique:email_servers,name,$id",
"address" => "required|unique:email_servers,address,$id",
"username" => "required",
"password" => "nullable|confirmed",
"security" => "nullable|exists:email_securities,value",
"host" => "required",
"port" => "required",
])->after(function ($validator) {
try{
if (request()->input('security')) {
$transport = Swift_SmtpTransport::newInstance(request()->input('host'), request()->input('port'), request()->input('security'));
}
else {
$transport = Swift_SmtpTransport::newInstance(request()->input('host'), request()->input('port'));
}
$transport->setUsername(request()->input('username'));
$transport->setPassword(request()->input('password'));
$mailer = \Swift_Mailer::newInstance($transport);
$mailer->getTransport()->start();
}
catch (Swift_TransportException $e) {
$validator->errors()->add('transport_exception', $e->getMessage());
}
catch (Exception $e) {
$validator->errors()->add('exception', $e->getMessage());
}
});
$email_server = EmailServer::findOrFail($id);
$email_server->update(array_filter(request()->all()));
activity()->by(auth()->user())->on($email_server)->withProperties($email_server)->log('Updated Email Server');
request()->session()->flash('status', 'Email Server updated!');
return redirect()->route('backend.email_servers');
}
現在,很明顯這不起作用爲$this->validate()
沒有after()
回調。
如何按照我想要的方式進行此項工作,以便如果我要使用Validator::make
,它將採用與$this->validate()
相同的方式?