![]() ![]() On the frontend, it would look something like this: Let’s say that I want to make my inventory software a bit more complicated by having an item field with two nested fields: name and description. This is personally my favorite way to expand validation, as I am able to neatly organize everything when I make custom rules, use After Validation Hooks, or expand rules, etc. $validator = Validator::make($request->all(), [Īnother way Laravel devs expand on validation is by separating validation from the controller with the use of form requests. ![]() One way they do this is by using the Validator object. Some Laravel devs choose to expand on this by using more complex methods of validation. However, if an XHR request is made (like ones that come from an API), a redirect will not be made but it will instead respond with JSON and a 422 HTTP status code. When the items above are validated, an HTTP redirect is made with the related error messages. These are just a few of the many rules that come pre-built into Laravel. For example, in the code block above, you will see max:255, which means that the item_name should not surpass 255 characters regex:/^+$/ means we only want alphanumeric characters. It’s amazing that, with some rules, you can provide more context as to what you want. Notice that beside each key (attribute) is a string where pipes separate all the rules that we want to validate the attribute. The code above is the simplest way we could validate requests in our controller. We need to validate these items when a POST request is made. In our form, we have fields for the item name, the SKU, and the price. Our model to access and manage our table is Product and the controller will be named ProductController ![]() In this software, we’ll store our items in the “Products” table of our database. Let’s say we’re building a very simple inventory software. Because of this, many devs choose to house their validation methods here, too. Usually, HTTP requests coming into Laravel are mostly handled in the controller (there are other places where requests are handled such as middleware, but that’s a discussion for another post). Validating arrays and nested attributesīefore we discuss validating arrays and nested arrays, let’s do an overview of the basics of Laravel validation.Luckily, Laravel provides many ways in which you can easily validate the data coming from your frontend. With the added complexity of frontend architecture, it’s more important than ever for developers to be able to submit more complex data to the backend.īecause we are building more complicated forms, we need more effective ways to validate and process the data coming from these forms. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |