Form Validationクラスを使う
Controller(form.php)
<?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’, ‘trim|required|min_length[5]|max_length[12]|xss_clean’);
$this->form_validation->set_rules(‘password’, ‘Password’, ‘trim|required|matches[passconf]’);
$this->form_validation->set_rules(‘passconf’, ‘Password Confirmation’, ‘trim|required’);
$this->form_validation->set_rules(‘email’, ‘Email’, ‘trim|required|valid_email’);if ($this->form_validation->run() == FALSE)
{
$this->load->view(‘myform’);
}
else
{
$this->load->view(‘myformok’);
}
}
}
?>
View(myform.php)
<html>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<head>
<title>My Form</title>
</head>
<body><?php echo validation_errors(); ?>
<?php echo form_open(‘form’); ?>
<h5>Username</h5>
<?php
$data = array(
‘name’=>’username’,
‘value’=>set_value(‘username’),
‘maxlength’ => ’50’,
‘size’ => ’50’
);
echo form_input($data);
?>
<h5>Password</h5>
<?php
$data = array(
‘name’=>’password’,
‘value’=>set_value(‘password’),
‘maxlength’ => ’50’,
‘size’ => ’50’
);
echo form_input($data);
?>
<h5>Password Confirm</h5>
<?php
$data = array(
‘name’=>’passconf’,
‘value’=>set_value(‘passconf’),
‘maxlength’ => ’50’,
‘size’ => ’50’
);
echo form_input($data);
?>
<h5>Email Address</h5>
<?php
$data = array(
‘name’=>’email’,
‘value’=>set_value(‘email’),
‘maxlength’ => ’50’,
‘size’ => ’50’
);
echo form_input($data);
?>
<div>
<?php
echo form_submit(‘submit’,’Submit’);
echo form_close();
?></body>
</html>