【问题标题】:Laravel 5.3 validation issueLaravel 5.3 验证问题
【发布时间】:2017-01-05 20:32:14
【问题描述】:

最近我从Laravel 5.2 升级了Laravel 5.3,之后我发现了几个验证问题。一个奇怪的问题是规则required_without,即在 laravel 5.2 上工作正常。

route 是:

Route::post('chats/create','ChatController@insertMessage');

在控制器内部,我将方法 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
    );
}

我使用 post-man 提出的请求如下:

请求标头是Authorizationcustom header,没有content-type 标头。我得到的response 是:

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

如果request 数据中存在content 字段,为什么它需要图像?

使用语句dd($input);我可以得到帖子数据:

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

【问题讨论】:

    标签: laravel laravel-5.3


    【解决方案1】:

    你可以使用验证规则,

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

    由于laravel 5.3 具有nullable 规则,您可以将其设置为您的输入值可以为空。查看您的代码,您将 imagecontent 设置为 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"
        )
    );
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2017-05-25
      • 2017-03-15
      • 1970-01-01
      • 2017-10-11
      • 1970-01-01
      • 2017-06-21
      • 2019-02-07
      • 2017-05-13
      相关资源
      最近更新 更多