Sonia Behal
Sonia Behal

Reputation: 73

Laravel request validation not working for postman request

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']
        ];
    }
}

Upvotes: 0

Views: 1384

Answers (2)

Muhammad Mahedi Hasan
Muhammad Mahedi Hasan

Reputation: 31

Set postman Header properly:

Content-Type: application/json

Accept: application/json

Upvotes: 3

Asim Al-Tayeb
Asim Al-Tayeb

Reputation: 41

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

Upvotes: 2

Related Questions