How can I use form request classes when I have different rules depending on the state of an object?
A simple example might be a post in a blog. We have three properties: title, text and state. If the state is "draft" only the title is required. If state is "published" title and text should be required.
You can use rules like required_when or required_if from Laravel, and/or you can have custom validation rules built which would access all the data from the request. Adding on my to-do list to write an article about this specific example, in a few weeks.
Form request works with any request. It doesn't matter if it's web, API or console route. As long as it is properly configured - it does work (and by configured, I mean that the request is properly made)
please can we get a link to the project file you are working with in this course so that following along can be moe efficient?
To be honest, I didn't save any repositories for this course because they were all separate small experiments, relatively easy to re-create if needed.
How can I use form request classes when I have different rules depending on the state of an object?
A simple example might be a post in a blog. We have three properties: title, text and state. If the state is "draft" only the title is required. If state is "published" title and text should be required.
You can use rules like
required_when
orrequired_if
from Laravel, and/or you can have custom validation rules built which would access all the data from the request. Adding on my to-do list to write an article about this specific example, in a few weeks.Ok published the article about it: Laravel Conditional Validation Based on Other Fields: 4 Examples
Hello. Can I reuse that form request in the Web, Api and console routes? I thought that form request only work for Web route.
Form request works with any request. It doesn't matter if it's web, API or console route. As long as it is properly configured - it does work (and by configured, I mean that the request is properly made)