2017-09-24 5 views
1

MailgunのAPI:私はこれの手順に従っててるLaravelカスタム検証 - 私はLaravelの新機能「カスタム検証ルール」のいずれかを実装しようとしていると私は、次のようなエラーに実行しているよ

Object of class Illuminate\Validation\Validator could not be converted to string 

ビデオ: New in Laravel 5.5: Project: Custom validation rule classes (10/14)

これはMailgun APIの電子メール検証ツールです。要求

シンプルなフォルム:姓、名、会社名、電子メールやメッセージ

をここでは私のコードは次のとおりです。

Route::post('contact', '[email protected]'); 

web.php

StaticPageController.php

use Validator; 
use App\Http\Validation\ValidEmail as ValidEmail; 

public function postContact(Request $request) { 
     return Validator::make($request->all(), [ 
      'firstname' => 'required|max:90', 
      'lastname' => 'required|max:120', 
      'company' => 'max:120', 
      'email' => [ 
       'required', 'string', 'max:255', 
       new ValidEmail(new \GuzzleHttp\Client) 
      ], 
      'message' => 'required', 
     ]); 
} 

ValidEmail.php私はこのような何かを見て期待してい

<?php 

namespace App\Http\Validation; 

use Illuminate\Contracts\Validation\Rule; 
use GuzzleHttp\Exception\GuzzleException; 
use GuzzleHttp\Client as Guzzle; 

class ValidEmail implements Rule 
{ 
    protected $client; 
    protected $message = 'Sorry, invalid email address.'; 

    public function __construct(Guzzle $client) 
    { 
     $this->client = $client; 
    } 

    public function passes($attribute, $value) 
    { 
     $response = $this->getMailgunResponse($value); 
    } 

    public function message() 
    { 
     return $this->message; 
    } 

    protected function getMailgunResponse($address) 
    { 
     $request = $this->client->request('GET', 'https://api.mailgun.net/v3/address/validate', [ 
      'query' => [ 
       'api_key' => env('MAILGUN_KEY'), 
       'address' => $address 
      ] 
     ]); 
     dd(json_decode($request->getBody())); 
    } 
} 

期待:すべてのヘルプははるかに高く評価されて

{ 
    +"address": "[email protected]" 
    +"did_you_mean": null 
    +"is_disposable_address": false 
    +"is_role_address": false 
    +"is_valid": false 
    +"parts": { 
     ... 
    } 
} 

。私はこの単純な例を2時間以上働かせようとしてきました。私の経験を持つ人が助けてくれることを願っています!お使いのコントローラで

+0

を、あなたはまだこれを必要なのでしょうか? – user3253002

答えて

0

これを試してみてください:

$validator = Validator::make($request->all(), [ 
    'firstname' => 'required|max:90', 
    'lastname' => 'required|max:120', 
    'company' => 'max:120', 
    'email' => [ 
     'required', 'string', 'max:255', 
     new ValidEmail(new \GuzzleHttp\Client) 
    ], 
    'message' => 'required', 
]); 


if ($validator->fails()) { 
    return redirect()->back() 
     ->withErrors($validator) 
     ->withInput(); 
} 

// if valid ... 
+0

私はこれを試しました。($ validator-> fails()){ リダイレクト() - > back() - > withErrors($バリデータ) - > withInput(); } else { "有効"を返します。 } 'と同じままです。 – sogeniusio

0

あなたのルートによると、postContact方法は、経路を処理する方法です。つまり、このメソッドの戻り値は、表示する応答でなければなりません。

Validatorオブジェクトを返すと、Laravelはその応答を文字列に変換しようとしています。バリデータオブジェクトは文字列に変換できません。

検証を行い、その検証に基づいて正しい応答を返す必要があります。 documenation hereでマニュアルバリデータについて詳しく読むことができます。要するに

、あなたはこのようなものが必要です。それは私が働いてしまった

public function postContact(Request $request) { 
    $validator = Validator::make($request->all(), [ 
     'firstname' => 'required|max:90', 
     'lastname' => 'required|max:120', 
     'company' => 'max:120', 
     'email' => [ 
      'required', 'string', 'max:255', 
      new ValidEmail(new \GuzzleHttp\Client) 
     ], 
     'message' => 'required', 
    ]); 

    // do your validation 
    if ($validator->fails()) { 
     // return your response for failed validation 
    } 

    // return your response on successful validation 
} 
+0

これと同様に試してみましょう。if($ validator-> fails()){ return_reirect() - > back() -> withErrors($ validator) -> withInput ; } else { "有効"を返します。 } ' – sogeniusio

関連する問題