Articles → PHP CODEIGNITER → Form Validation In Codeigniter
Form Validation In Codeigniter
Steps
- Create a textbox and a "submit" button in a view.
- Load the "Form" helper in the controller class.
- Load the "form_validation" library in the controller class.
- Apply form validation rules using "set_rules" method.
- Call "run" method to validate data.
View Changes
<body>
<div id="container">
<div id="body"></div>
</div>
<?php echo form_open();
echo form_input('username', ");
echo form_submit('mysubmit', 'Click Me');
echo form close();?>
</body>
</html>
Controller Changes
- Load the "Form" helper in controller class.
- Load the "form_validation" library in controller class.
- Apply form validation rules using "set_rules" method.
- Call "run" method to validate data.
public function Index() {
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->view('users');
$this->form validation->set rules('username', 'Username', 'required');
if ($this->form_validation->run() == FALSE) {
echo "Please enter user name";
}
else{
echo "Valid user name";
}
}
Output
Click to Enlarge
Click to Enlarge