Have the route out of auth guard.
Jul 28, 2021
5
Level 1
API request validation
I have created a form request to make validation logic there instead in controller.
class StoreReviewRequest extends FormRequest{}
This route should be open, available to everyone, I don't have users to authorize them. If I set false in authorize method I can't access the method in controller and I have message "403 Forbidden". If I set it to true it redirects me to the login page.
How should I "disable" authorization in form request?
public function authorize()
{
return true;
}
Level 1
That was a good starting point to fixing this problem. Thank you!
Anyway, this is the solution. I have overrided the function failedValidation to return errors in json format. So if anyone needs:
protected function failedValidation(Validator $validator)
{
throw new HttpResponseException(response()->json($validator->errors(), 422));
}
And here is the whole StoreReviewRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Response;
class StoreReviewRequest 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 [
'full_name' => 'required_if:isAnonymous,true|string',
'email' => 'required_if:isAnonymous,true|email',
'job_position' => 'string',
'review' => 'string',
'overall_grade' => 'required|numeric',
'program_grade' => 'required|numeric',
'help_grade' => 'required|numeric',
'instructors_grade' => 'required|numeric',
'communication_grade' => 'required|numeric',
'generation' => 'required|integer'
];
}
protected function failedValidation(Validator $validator)
{
throw new HttpResponseException(response()->json($validator->errors(), 422));
}
}
Please or to participate in this conversation.