When I try to use form validation to validate data from clients, I cannot validate it correctly because the method authorize()is executed even if the validation rules contained in rules()are violated.
This is the code:
class PostAssignRequest extends Request {
public function authorize()
{
$params = $this->request->all();
if(User::find($params['userId'])->company != Auth::user()->company)
{
return false;
}
return true;
}
public function rules()
{
$rules = [
'userId' => 'required|exists:users,id',
'taskId' => 'required|exists:tasks,id'
];
return $rules;
}
}
This example User::find()runs even if the parameter is userIdnot set.
I also noticed that if the method authorize()just returns true, then I get a response 422telling me that the parameter is userIdmissing.
How should I deal with this? First, I would like to check the rules, and then, if they are ok, log in.
source
share