Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now that the two views of the controller have been set up, I now have something that I can use the Form Validation Library with. Here's the control flow at a high level. First, I'll load the form_validation library. Then, I'll set the validation rules for each form element. When that's complete, I'll execute the validator to see if it passed or not. With that answer, the application can decide what to do based on validation success. I'm going to get started by returning to the IDE. I'll open the magazine controller, and navigate to right before the view is displayed.
I'll start by loading the form validation library. So, this, load, library, with a name, form, underscore, validation. Next I'll call a method called set rules. Set rules takes an array of arrays that contain three key value pairs. The field which is the form fields name, a label which is a human readable label that will be shown on failures and rules. A string containing the names of form validation call back functions. There's a complete list of validators that ship with CodeIgniter at the following URL.
For the time being we'll start slow and build up. This form validation set underscore rules. I'll pass an array. The first array will be for publication Id. Array, field, publication_id and then the label will just be publication. The publication will be required. Fortunately there's a validator callback named required which will just check to see if content has been set.
So, rules, required. That was pretty straightforward. I'll add a validator for the issue_number as well. Array, field, issue_number, label Issue number. This time for the rules I'm going to chain two validators together using another validator that ships with CodeIgniter that checks if input is numeric.
To specify multiple validators separate it with a pipe character, so required and then a pipe and then is underscore numeric. Finally, the publication date. Array, field, issue, date, publication. The label is publication date. Now, there are no date validators that ship with CodeIgniter, so I'll use a custom callback.
To do that, start with rules. The publication date is required, but for the custom callback. I'll need to start the method name with a callback_ followed by the name of the actual method, which I'll call date_validation. The callback is a method in the current controller, which I'll create in a moment. By default, the error messages are wrapped in a paragraph tag. Let's spruce things up a little bit by setting the error message to limiters. The method takes two arguments. The opening and the closing delimiter.
This form validation. Set_error_delimiters. Have an opening DIV for the class of alert, and alert-error, then followed by a closing DIV. Finally, I'll execute the form validator using the method run. This, form, validation, run. It returns Boolean, true or false, so I can use it with an if statement.
So we'll start off if the form does not validate. We'll show the regular form else if the form did validate, this, load, view, magazine form success. Now that we've completed the logic so far, let's create a callback function to validate the date. Within the controller, create a new public function called date validation that takes a string input. Add a little bit of documentation.
This is a date validation call back, takes a string and it returns a Boolean value. True if it passes, fault if it fails. I'm going to use the PHP function check date for validation. To do that I'll explode the input on dash, so test date equals explode and then input. So if then not and I'm going to surpress errors and warnings from check eight.
Test date 1, test date 2, and then test date 0. So if it fails that check I'm going to use one of the methods in form validation known as sent message. So this form validation set message, which takes two arguments. The first is a string containing the field name, which in this case is, date validation, followed by a string containing the message, itself. The, and I can use a percent, S, that will be replaced with the human readable label set earlier.
So the percent S field must be in year, month, day, format. Since it failed return Boolean false, otherwise it succeeded so return true. Save the controller and switch to the browser. For the issue number type A which won't validate and click save. There's no message, because the form validation method doesn't produce any automatic output. Which gives developer control over how the application performs. Go back to the IDE, and open the Magazine Form view. At the very top, above the form itself, I'm going to Echo a function provided by the validation library that will render the errors, validation errors. Save then return to the browser and try filling validation again. This time I see the error messages I was expecting.
Lets try to pass validation, so, issue number three. And it was published on 2013, March 1st. Click save. Magazine created, success. Except it didn't actually create it, because one more piece is needed. Persistence.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 105106 Viewers
56 Video lessons · 116890 Viewers
71 Video lessons · 86102 Viewers
131 Video lessons · 41178 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.