As its title suggests, this post is a continuation of the previous post Simple CRUD with CodeIgniter. If previously we've made a simple CRUD program, now we will add to it with the form validation and message information.
As before, we have to set the configuration in the file library and helper autoload.php. This time kitambahkan form_validation library and session.
1.$autoload['libraries'] = array('database', 'form_validation', 'session');
2.$autoload['helper'] = array('form', 'url');
As for the configuration database in database.php file remains the same.
1. $db['default']['hostname'] = 'localhost';
2. $db['default']['username'] = 'root';
3. $db['default']['password'] = '';
4. $db['default']['database'] = 'ci';
The database name used is "ci" and the name of the table "cd"....
CREATE DATABASE ci;
USE ci; CREATE TABLE `cd` (
`kode_cd` int(11) NOT NULL auto_increment,
`judul` varchar(50) default NULL,
`kategori` varchar(25) default NULL,
`stok` int(11) default '0',
`harga` int(11) default '0',
PRIMARY KEY (`kode_cd`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
Coding models on file crud_model.php also remained the same.
While coding for the controller is in the file crud.php there is a change. With form_validation library we can define rules for each field. For example, the field "title" must not be empty, the field "stock" must be a positive integer, and so on.
Coding crud_view.php view the file also contained perbuahan. To display the error message for each field, we add the code As below.
After all the coding on the model, controller and view are wrong, then we run the application by typing in the browser http://localhost/ci/index.php/crud. Then it will perform CRUD application page as below.
After that we try to incorporate input validation testing empty, if validation success then it will appear as below.
FYI, in addition to display the error message in each field, CodeIgniter can also display all the error messages manjadi 1. The way is to write a single line of code that is only ,
Then automatically all the error messages will be tempil to 1. For more clearly seen in http://codeigniter.com/user_guide/libraries/form_validation.html.