Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Validating user input with the form validation library

From: Up and Running with PHP CodeIgniter

Video: Validating user input with the form validation library

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.

Validating user input with the form validation library

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.

Show transcript

This video is part of

Image for Up and Running with PHP CodeIgniter
Up and Running with PHP CodeIgniter

23 video lessons · 5641 viewers

Jon Peck
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Up and Running with PHP CodeIgniter.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.