Laravel request validation not working for postman request

1.2k Views Asked by At

For the following request, when body params are sent as JSON, it always validates the request (as in no validation rule is triggered), but when sent in form-data or form-urlencoded, it passes through validation rules. Is it a limitation with Laravel?

namespace App\Api\Requests\OrganizationUser;

use App\Api\Constants\PlatformRoles;
use Framework\Http\Requests\APIFormRequest;

class CreateNewUserRequest extends APIFormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'bail|required|string|max:255',
            'access' => 'present|array',
            'access.*.access_type' => ['bail', 'sometimes', 'string'],
            'access.*.id' => ['bail', 'sometimes', 'string']
        ];
    }
}
2

There are 2 best solutions below

0
Asim Al-Tayeb On

couz when you send it as form-data the body structure were changed try to dd your request and see how the request look like.

public function rules()
{
   dd($this->request->all());
0
Muhammad Mahedi Hasan On

Set postman Header properly:

Content-Type: application/json

Accept: application/json