Categories CodeIgniter

Validation in CodeIgniter

Step 1 : Make View in view folder as “myform.php”
Step 2 : Make controller in controller folder as “Form.php”
Step 3 : Make another view in view folder as formsuccess.php to show after successful validation.

myform.php file

<html>
<head>
<title>My Form</title>
</head>
<body>
<?php echo form_open(‘form’); ?>
<h5>Username</h5>
<input type=”text” name=”username” value=”<?php echo set_value(‘username’); ?>” size=”50″ />
<?php echo form_error(‘username’, ‘<div class=”error”>’, ‘</div>’); ?>

<h5>Password</h5>
<input type=”text” name=”password” value=”<?php echo set_value(‘password’); ?>” size=”50″ />
<?php echo form_error(‘password’, ‘<div class=”error”>’, ‘</div>’); ?>

<h5>Password Confirm</h5>
<input type=”text” name=”passconf” value=”<?php echo set_value(‘passconf’); ?>” size=”50″ />
<?php echo form_error(‘passconf’, ‘<div class=”error”>’, ‘</div>’); ?>

<h5>Email Address</h5>
<input type=”text” name=”email” value=”<?php echo set_value(’email’); ?>” size=”50″ />
<?php echo form_error(’email’, ‘<div class=”error”>’, ‘</div>’); ?>
<h5>Gender</h5>
<input type=”radio” name=”gender” value=”Male” checked=”” />
<input type=”radio” name=”gender” value=”Female”/>
<?php echo form_error(‘gender’, ‘<div class=”error”>’, ‘</div>’); ?>
<h5>Qualification</h5>
<select name=”qualifiction”>
<option>Select any one</option>
<option>High School</option>
<option>Inter</option>
<option>BA</option>
<option>BCA</option>
<option>BBA</option>
</select>
<?php echo form_error(‘qualifiction’, ‘<div class=”error”>’, ‘</div>’); ?>
<h5>Color</h5>
<input type=”checkbox” name=”options[]” value=”red” />Red
<input type=”checkbox” name=”options[]” value=”blue” />Blue
<input type=”checkbox” name=”options[]” value=”green” />Green
<?php echo form_error(‘options[]’, ‘<div class=”error”>’, ‘</div>’); ?>
<div><input type=”submit” value=”Submit” /></div>
</form>
</body>
</html>

Form.php
<?php class Form extends CI_Controller {
public function index(){
$this->load->helper(array(‘form’, ‘url’));
$this->load->library(‘form_validation’);
$config = array(
array(
‘field’ => ‘username’,
‘label’ => ‘Username’,
‘rules’ => ‘required’
),
array(
‘field’ => ‘password’,
‘label’ => ‘Password’,
‘rules’ => ‘required’,
‘errors’ => array(
‘required’ => ‘You must provide a %s.’,
),
),
array(
‘field’ => ‘passconf’,
‘label’ => ‘Password Confirmation’,
‘rules’ => ‘required’
),
array(
‘field’ => ’email’,
‘label’ => ‘Email’,
‘rules’ => ‘required’
),
array(
‘field’ => ‘gender’,
‘label’ => ‘Gender’,
‘rules’ => ‘required’
),
array(
‘field’ => ‘qualifiction’,
‘label’ => ‘Qualifiction’,
‘rules’ => ‘required’
),
array(
‘field’ => ‘options[]’,
‘label’ => ‘Options’,
‘rules’ => ‘required’
)
);
//$this->form_validation->set_rules(‘options[]’, ‘Options’, ‘required’);
$this->form_validation->set_rules($config);
if ($this->form_validation->run() == FALSE){
$this->load->view(‘myform’);
}else{
$this->load->view(‘formsuccess’);
}
}
}
formsucess.php

<html>
<head>
<title>My Form</title>
</head>
<body>

<h3>Your form was successfully submitted!</h3>

<p><?php echo anchor(‘form’, ‘Try it again!’); ?></p>

</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *