Custom Error Messages2:30 with Jonathan Barrios
In this video, we'll explore how Laravel validates our application's incoming data. By default, Laravel's base controller class uses ValidatesRequests which provides a convenient method to validate incoming HTTP requests with a variety of powerful validation rules. See you there!
Excellent job refactoring your routes. 0:00 In this video, we'll cover how validators work inside of the author controller.php 0:03 file by sending a post request with missing fields that are required. 0:09 In the next section, we'll add our own custom messages inside the store function 0:14 as an argument to the validator make method. 0:19 Let's get started. 0:23 Navigate to the author controller.php file, 0:25 and let's create some custom validation messages for our API. 0:28 First, make sure you import the validator at the top like this. 0:33 Next, let's build some custom validation messages using the validator facade 0:38 on the store function like this. 0:43 Next, let's validate the update function like this. 0:51 Let's test our custom validation messages like this. 1:08 Now when we send a POST request with a missing name field, for example, 1:25 we get a custom error message. 1:29 How cool is that? 1:31 How about a POST requests with only a name field? 1:34 We get three custom validation messages. 1:46 Notice how both times we return the HTTP status code we expected. 1:51 400 bad request. 1:56 Finally, let's do the same for 1:59 the store function in the book controller.php file like this. 2:01 Next, let's validate the update function. 2:16
You need to sign up for Treehouse in order to download course files.Sign up