2017-07-16 104 views
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()相同的方式?

回答

0

我想通了通過看ValidatesRequests特點:

$validator = Validator::make(request()->all(), [ 
     "name" => "required|unique:email_servers", 
     "address" => "required|unique:email_servers", 
     "username" => "required", 
     "password" => "required|confirmed", 
     "security" => "required|exists:email_securities,name", 
     "host" => "required", 
     "port" => "required", 
    ]); 
    $validator->after(function ($validator) { 
     $transport_status = $this->transportStatus(); 

     if ($transport_status != 'OK') { 
      $validator->errors()->add('transport_status', $transport_status); 
     } 
    }); 

    if ($validator->fails()) { 
     $this->throwValidationException(request(), $validator); 
    } 
    else { 
     $email_server = EmailServer::create(request()->all()); 

     activity()->by(auth()->user())->on($email_server)->withProperties($email_server->toArray())->log('Created Email Server'); 
     request()->session()->flash('status', 'Email Server created!'); 

     return redirect()->route('backend.email_servers'); 
    } 

public function transportStatus() 
{ 
    try { 
     $security = (request()->input('security') != 'None') ? request()->input('security') : null; 
     $transport = Swift_SmtpTransport::newInstance(request()->input('host'), request()->input('port'), $security); 
     $transport->setUsername(request()->input('username')); 
     $transport->setPassword(request()->input('password')); 
     $mailer = \Swift_Mailer::newInstance($transport); 
     $mailer->getTransport()->start(); 

     return 'OK'; 
    } 
    catch (Swift_TransportException $e) { 
     return $e->getMessage(); 
    } 
}