Laravel API vs Web Validation Errors: How Does It Work?

preview_player
Показать описание
I've received a few questions about using Validation Form Requests for API and how to handle the response if it's returned in HTML for some reason. So let me explain how it works.

- - - - -
Support the channel by checking out our products:
Рекомендации по теме
Комментарии
Автор

Started with Laravel a couple of weeks ago, and im learning a lot with your channel, thx for all the effort you put in your videos.

AnDrU
Автор

Your video came at the perfect time. I am testing an api and all the responses are html so far. I know what to do now.

william
Автор

You are a superb person, as always. Now, I feel that I follow a great person 😍😍😍😍😍😍

bmtamim
Автор

Thank you! This is just what I was searching for.

markoperica
Автор

Indeed great tutorial, Thank you so much for share amazing videos

Akshaykumarojha
Автор

Thank you very much, you helped me a lot.

ArthurRamires
Автор

Thank you, the "Accept application/json" did the trick.

AliAwwad
Автор

great, thank for take your time and respond me, best regard

haroldpepete
Автор

thank you. With this video. I got an answer to my comment too

msdeav
Автор

Always Learning from your video. <3

fftfaisal
Автор

For API responses I use a dedicated middleware that checks for the headers and take a look if the requested json is also valid. There were many times people were using invalid json, forgetting to put a comma or putting a comma after the last field. Not everyone uses Postman or insomnia that shows that before requesting.

JohnnyBigodes
Автор

At the end of the video, the rule does work that way or should it actually be $rules['device_name'] = 'required' ? Anyway, nice tip, thanks for the videos.

vitorfontoura
Автор

I use an "AcceptsJson" middleware in my API middleware group to force Laravel to render as JSON at any given time. Even if the client does not explicitly request it, because it does make absolutely no sense to return HTML from an API route.

Just a simple $request->headers->set('Accept', 'application/json'); et voilà.

mabdullahsari
Автор

I'm gonna throw a few more questions your way since I like the current line of topics you are covering. Great Job and Thanks a Lot. Helps us little guys out a lot.

1. So say I have a validation for store method that accepts an image along with some other data (e.g name and email). Should look like this:
$request->validate([
'name' => ['required', 'string', 'min:3', 'max:255'],
'email => ['required', 'string', 'min:3', 'max:255'],
'image' => ['required', 'image', 'max:3000'],

]);
But I won't allow the the user to update the image after store. It is a one time thing only. So the validation in update method looks like this:
$request->validate([
'name' => ['required', 'string', 'min:3', 'max:255'],
'email => ['required', 'string', 'min:3', 'max:255'],

]);
Should I make 2 separate request classes or is there an easier way to handle this with a single request class ??

2. So I have a registration that, alongside some other rules, will check for a unique e-mail address in the database like this:
$request->validate([

'email => ['required', 'string', 'min:3', 'max:255', 'unique:users, email_address'],

]);

But say (with a grain of salt. This is just an example) I will no longer validate email asunique in the update method. Maybe because I am allowing duplicate e-mails for multiple accounts owned by the same person(say I can somehow check that and approve). So my update method validation looks like this:
$request->validate([
'email => ['required', 'string', 'min:3', 'max:255'],

]);
So basically my store method email validation has one extra rule(unique).
Again, do I need to make 2 separate request classes or can I somehow use one single request class to accomplish this ?

atiquechowdhury
Автор

expects json is one of those that you dont think its needed or done know about it until you then you never forget.

shoemoney
Автор

This is why i love laravel, it's all so easy to make it do what you want.

Stoney_Eagle
Автор

Is this will give exact error instead of home page in postman without giving application/json in postman header?

francissebin
Автор

I just add a custom middleware that forces the API routes to always expect json and thus return json response.

BsiennKhan
Автор

Amazing video. 🙌🏼

Please can you do something on Elastic Search

josephemeruwa
Автор

Great hint.
In the end you mistyped. Should be $rules['device_name'] = 'required'; Otherwise it adds a '0' key to $rules with value of array.

alexaverkiyev