dongyongju9560
2018-06-04 05:32
浏览 203
已采纳

使用Laravel 5.6 api验证JSON Post

Having a struggle with validating JSON input for an api on Laravel 5.6. I've been trying the solution at How to get and validate application/json data in Laravel? 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? What am I missing that it doesn't pick up the fields as they are getting to the controller in the $request?

Any guidance would be greatly appreciated.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douxidao3524 2018-06-04 05:48
    已采纳

    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):

    $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.

    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

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题