2017-01-05 5 views
1

Недавно я обновил Laravel 5.3 от Laravel 5.2, после чего я нашел несколько вопросов проверки. Одна из странных проблем связана с правилом required_without i.e. отлично работает с laravel 5.2.Laravel 5.3 validation issue

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

Запрос он aders Authorization и custom header, нет content-type Заголовок есть. И response я получаю:

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

Почему это требует изображение, если content поле присутствует там в данных request?

Используя заявление dd($input); я могу получить почтовые данные, как:

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

ответ

1

Вы можете использовать правило проверки, как,

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

С laravel 5.3 имеют nullable правило, вы можете установить его там, где ввод значение может быть нулевым. Изучив свой код, вы устанавливаете image и content как null, если нет в запросе.

"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" 
    ) 
); 

Надеется, что это помогает !!