2017-01-05 5 views
1

最近、私はLaravel 5.3Laravel 5.2からアップグレードしました。その後、いくつかの検証の問題が見つかりました。奇妙な問題の1つは、ルールrequired_withoutです。つまり、laravel 5.2で正常に動作します。Laravel 5.3の検証に関する問題

routeは次のとおりです。

Route::post('chats/create','[email protected]'); 

コントローラ内部では、私がした方法insertMessage()を定義しました:

public function insertMessage(Request $request){ 
    $error = false; 
    $result = array(); 
    $responsecode = 200; 
    $input = $request->all(); 

    $validator = Validator::make(
        array(
         "patient_id" => $input["patient_id"], 
         "chat_id" => !empty($input["chat_id"]) ? $input["chat_id"] : null, 
         "sender_id"=> $input["sender_id"], 
         "image" => $request->hasFile('file') ? $request->file('file') : null, 
         "content" => !empty($input["content"]) ? $input["content"] : null 
        ), 
        array(
         "patient_id" => "required|exists:users,id,user_type,patient", 
         "chat_id" => "sometimes|nullable|exists:chats,id", 
         "sender_id"=>"required|exists:users,id", 
         "image" => "required_without:content|image", 
         "content" => "required_without:image" 
        ) 
       ); 

    if ($validator->passes()){ 
     try { 
      DB::beginTransaction(); 
      //Some operations 

     } 
     catch(\Exception $e){ 
      //Some operations 
     } 
    } 
    //Finally return the response 
    return response()->json(array(
     'error' => $error, 
     'input_data' => $result), 
     $responsecode 
    ); 
} 

そして私はポストマンを使用して作っています要求としてある:

enter image description here

広告主はAuthorizationであり、custom headerであり、content-typeヘッダーはありません。そして、私は取得していますresponseは次のとおりです。

{ 
    "error": true, 
    "input_data": { 
    "error_message": "The image must be an image. " 
    } 
} 

contentフィールドはrequestデータであっ存在する場合、それはイメージが必要なぜ?あなたはlaravel 5.3nullableルールを持っているので、あなたがどこ入力に設定して

"image" => "nullable|required_without:content|image", 
"content" => "nullable|required_without:image" 

、として検証ルールを使用することができ

array:4 [ 
    "patient_id" => "3" 
    "content" => "Hello" 
    "sender_id" => "1" 
    "sender_name" => "System Admin" 
] 

答えて

1

:文dd($input);を使用して

私はとポストデータを取得することができますvalueにはnullを指定できます。あなたのコードを調べると、リクエストに存在しない場合、nullとしてimagecontentが設定されています。

"image" => $request->hasFile('file') ? $request->file('file') : null, 
"content" => !empty($input["content"]) ? $input["content"] : null 

だから、最終的にあなたのvalidator

$validator = Validator::make(
    array(
     "patient_id" => $input["patient_id"], 
     "chat_id" => !empty($input["chat_id"]) ? $input["chat_id"] : null, 
     "sender_id"=> $input["sender_id"], 
     "image" => $request->hasFile('file') ? $request->file('file') : null, 
     "content" => !empty($input["content"]) ? $input["content"] : null 
    ), 
    array(
     "patient_id" => "required|exists:users,id,user_type,patient", 
     "chat_id" => "sometimes|nullable|exists:chats,id", 
     "sender_id"=>"required|exists:users,id", 
     "image" => "nullable|required_without:content|image", 
     "content" => "nullable|required_without:image" 
    ) 
); 

は、この情報がお役に立てば幸いです、のようになります!

関連する問題