简体   繁体   English

使用Laravel 5.6 API验证JSON Post

[英]Validation of JSON Post using Laravel 5.6 api

Having a struggle with validating JSON input for an api on Laravel 5.6. 在Laravel 5.6上为API验证JSON输入时费力。 I've been trying the solution at How to get and validate application/json data in Laravel? 我一直在尝试如何在Laravel中获取和验证应用程序/ json数据的解决方案 but still doesn't resolve. 但仍然无法解决。

The supporting class: 配套班:

<?php
namespace App\Http\Controllers\API;

class ResponseObject
{
    const status_ok = "OK";
    const status_fail = "FAIL";
    const code_ok = 200;
    const code_failed = 400;
    const code_unauthorized = 403;
    const code_not_found = 404;
    const code_error = 500;

    public $status;
    public $code;
    public $messages = array();
    public $result = array();
}

and the Controller: 和控制器:

namespace App\Http\Controllers\API;

use App\Http\Resources\MyItemsResource;
use App\MyItem;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use \Illuminate\Http\Response;
use \Illuminate\Support\Facades\Response as FacadeResponse;
use \Illuminate\Support\Facades\Validator;
use App\Http\Controllers\API\ResponseObject;



class MyItemsController extends Controller
{
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request)
    {
        $response = new ResponseObject;

        /*
        sending a body of
        {"code": "45678", "description": "My great item"}

        then:
        // print_r($request->json()->all()); die();

        produces:
            Array
            (
                [code] => 45678
                [description] => My great item
            )
         so data is getting to server
        */

        $validator = Validator::make($request->json()->all(), [
            'code', 'required',
            'description', 'required',
        ]);

        if($validator->fails()){
            $response->status = $response::status_failed;
            $response->code = $response::code_failed;
            foreach ($validator->errors()->getMessages() as $item) {
                array_push($response->messages, $item);
            }
        } else {
            $myItem = new MyItem();
            $myItem->code = $request->code;
            $myItem->description = $request->description;
            $response->status = $response::status_ok;
            $response->code = $response::code_ok;
            $response->result = $myItem;
        }

        return FacadeResponse::json($response);
        /*
         Returns:
        {
            "status": "FAILED",
            "code": 400,
            "messages": [
                [
                    "The 1 field is required."
                ],
                [
                    "The 3 field is required."
                ]
            ],
            "result": []
         }
         */
    }
}

Why does it refer to the fields as 1 field and 3 field? 为什么将字段称为1字段和3字段? What am I missing that it doesn't pick up the fields as they are getting to the controller in the $request? 我想念的是,由于它们在$ request中到达控制器时,它没有拾取字段?

Any guidance would be greatly appreciated. 任何指导将不胜感激。

It refers to the keys of the validation rules. 它指的是验证规则的关键。

Here you just have an array of 4 words (indexes 1 and 3 are valid rules that fail): 在这里,您只有4个字的数组(索引1和3是失败的有效规则):

$validator = Validator::make($request->json()->all(), [
    'code', 'required',
    'description', 'required',
]);

^ This equals to: ^等于:

$validator = Validator::make($request->json()->all(), [
    0 => 'code', 
    1 => 'required',
    2 => 'description', 
    3 => 'required',
]);

And looks for inputs 0 , 1 , 2 , 3 . 并查找输入0123

Instead you should make an associative array: 相反,您应该创建一个关联数组:

$validator = Validator::make($request->json()->all(), [
    'code' => 'required',
    'description' => 'required',
]);

^ This will look for inputs code and description and validate them with the rule required ^这将查找输入codedescription ,并使用所需规则required验证

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM