You have the rule defined?
Custom validation rule not working
Hi folks!
Something very strange is happening. I'm writing an API with the latest version of Laravel, and the custom validation aren't working! The code from the rule doesn't even touched!
Even this code is not working
Route::post('test', function (Request $request) {
$validator = \Validator::make($request->all(), [
'title' => [
'required',
function ($attribute, $value, $fail) {
if ($value === 'foo') {
$fail('The '.$attribute.' is invalid.');
}
},
],
])->validate();
});
In this example, only 'required' rule is applied, and, yes, i'm sending the 'title' in request.
Anyone can help me?
@geninoliveira In that case add an Accept header in your API call. If the form request sees it's an XHR request, the error message is returned as JSON.
If the request was an XHR request, an HTTP response with a 422 status code will be returned to the user including a JSON representation of the validation errors.
Accept: application/json
And that is verbatim from a section in Creating Form Requests just before the "Adding After Hooks To Form Requests" section.
Please or to participate in this conversation.


