Codeigniter Form Validation : Part One

GEEK ALERT:  If you’re not a web developer who uses Codeigniter then this blog will not interest you.   I’ll go back to my usual chatty vibes soon.

If you ARE a web developer and you’re interested in Codeigniter then this is the beginning of my attempt to clarify how advanced form validation works.  As far as I can tell nobody from the Codeigniter community has really explained this properly yet and published the code for working examples.  So, here I go…

Form Validation

Form validation on Codeigniter requires the following FOUR files:

One view file for displaying the form

One controller file for checking the form

One model file for creating, reading, updating and deleting database records
(aka CRUD)

One view file for the success page

WARNING: Do NOT try to simplify this file structure or mess with it in any way at all. If you do then your scripts will misbehave, you’ll waste lots of time and you’ll basically be very miserable.

Part 1 : The Form

Build a file called myform.php and save it onto your views folder:

<html>

<head>

<title>My Form</title>

</head>

<body>

<?php echo validation_errors();
?>

<?php echo form_open(‘form’);
?>

<h5>Username</h5>

<input type=”text” name=”username” value=”<?php
echo set_value(‘username’); ?>” size=”50″ />

<h5>Password</h5>

<input type=”text” name=”password” value=”<?php
echo set_value(‘password’); ?>” size=”50″ />

<h5>Password Confirm</h5>

<input type=”text” name=”passconf” value=”<?php
echo set_value(‘passconf’); ?>” size=”50″ />

<h5>Email Address</h5>

<input type=”text” name=”email” value=”<?php
echo set_value(’email’); ?>” size=”50″ />

<div><input type=”submit”
value=”Submit” /></div>

</form>

</body>

</html>

IMPORTANT: Notice how the set_value part helps us to repopulate the form in
the event on an error.

Part 2 : The Success Page

You shouldn’t have any problems with this. Make up your success page, call
it formsuccess.php and chuck it in the views folder.

<html>

<head>

<title>My Form</title>

</head>

<body>

<h3>Well done mad skull!</h3>

<p>Your form was successfully submitted</p>

</body>

</html>

Part 3: The Controller

Here comes the tricky bit. The code below does a bunch of checks on your submitted
form variables and then either sends you back to the form (if there’s any error)
or sends you to a success page if the form is filled out properly.

IMPORTANT: Check out the callback function. That’s a customised check that’s
been created. In this case, the callback makes sure the username does not equal
“test”. Hardly the kind of thing you’d do in real life but the customised
callback functionality will be important when we start doing fancy things with
our database later on.

<?php

class Form extends Controller {

function index()

{

$this->load->helper(array(‘form’, ‘url’));

$this->load->library(‘form_validation’);

$this->form_validation->set_rules(‘username’, ‘Username’, ‘callback_username_check’);

$this->form_validation->set_rules(‘password’, ‘Password’, ‘required’);

$this->form_validation->set_rules(‘passconf’, ‘Password Confirmation’,
‘required’);

$this->form_validation->set_rules(’email’, ‘Email’, ‘required’);

if ($this->form_validation->run() == FALSE)

{

$this->load->view(‘myform’);

}

else

{

$this->load->view(‘formsuccess’);

}

}

function username_check($str)

{

if ($str == ‘test’)

{

$this->form_validation->set_message(‘username_check’, ‘The %s field can
not be the word “test”‘);

return FALSE;

}

else

{

return TRUE;

}

}

}

?>

COMING UP…

Everything I’ve written so far is just taken from the Codeigniter user guide
with just a few comments, from me, here and there. Next time I’m going to go
beyond the manual and do a working example that actually adds a record to the
database. We’re also going to create a customised callback that makes sure we
have a unique username. Rock on!

Advertisements

2 comments

  1. its not suffient buddy to work it on. its giving helper/cntrler_name_helper.php not found error

    1. parkerandhobbes · · Reply

      Hi,

      Sorry about that. This article is really old and taken from an old version of Codeigniter. It’s possible that what you’re reading it out of date. I apologise for that. I would recommend heading to YouTube to check out my video training series which covers form validation and a whole lot more. It’s ten videos in total. Each video is very short and the whole lot lasts for less than two hours. You can find details about my free video training series here.

      I hope this helps you.

      -DC

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: