skip to Main Content

I am using default Request for a public method in controller, when I use validation required and don’t pass the required params in api payload with postman , it just redirect me to login page and does not show any validate rule message.

my method :

class OrderController extends Controller
{
    public function createOrder(Request $request)
    {
        $this->validate($request,[
            'name' => 'required',
            'phone' => 'required',
            'address' => 'required',
            'wallet_id' => 'required'
        ]);
        return 'something';
    }
}

with this route:

Route::post('/createOrder', 'OrderController@createOrder')->middleware('auth:api');

in postman (or application) when I don’t send one of required parameters, it redirect me to login page.

thanks in advance

2

Answers


  1. Chosen as BEST ANSWER

    I found it, just need to create a custom request and make method failedValidation in the custom request and return HttpResponseException like below:

    <?php
    
    namespace AppHttpRequests;
    
    use IlluminateFoundationHttpFormRequest;
    use IlluminateContractsValidationValidator;
    use IlluminateHttpExceptionsHttpResponseException;
    
    class CreateShoppingRequest extends FormRequest
    {
    /**
     * 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' => 'required',
            'phone' => 'required',
            'address' => 'required',
            'wallet_id' => 'required'
        ];
    }
    
    public function failedValidation(Validator $validator)
    {
        throw new HttpResponseException(response()->json([
            'data' => null,
            'status' => 'validation_error',
            'message' => 'error on insert required data',
            'validation' => $validator->errors()
        ], 400));
    }
    }
    

    and in my controller implement the custom request:

    use AppHttpRequestsCreateShoppingRequest;
    class OrderController extends Controller
    {
        public function createOrder(CreateShoppingRequest $request)
        {
            $this->validate($request,[
                'name' => 'required',
                'phone' => 'required',
                'address' => 'required',
                'wallet_id' => 'required'
            ]);
            return 'something';
        }
    }
    

  2. In Postman, in your request, in Headers tab set Accept key and application/json as value. In this way your Authenticate middleware should avoid redirection in case you are not authenticated.

    The default Authenticate middleware:

    class Authenticate extends Middleware
    {
        /**
         * Get the path the user should be redirected to when they are not authenticated.
         */
        protected function redirectTo(Request $request): ?string
        {
            return $request->expectsJson() ? null : route('login');
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search