Form Validation CodeIgniter

Form Validation CodeIgniter

Apa Itu Form Validation?

Validasi form biasanya dieksekusi pada server. Validasi akan dilakukan setelah data form diisi dan tombol submit ditekan pada komputer. Jika satu atau beberapa data form masih mengandung kesalahan, maka server akan mengembalikan semua data dan meminta komputer untuk mengirimkan kembali data yang benar.

Validasi form biasanya melakukan 2 fungsi:

• Validasi dasar: Memeriksa semua isi form apakah semua data sudah diisi atau belum. Proses ini akan memeriksa satu persatu disetiap isi form dan memeriksa datanya.

• Validasi format data: Memeriksa kebenaran format dan nilai data yang dimasukkan. Untuk proses pemeriksaannya membutuhkan perintah javascript yang lebih kompleks.

Pada Framework CodeIgniter form validasi sudah disiapkan, tugas programmer hanya mengikuti dan memanggil fungsi yang sudah tersedia di codeigniter. Baris program juga hanya sedikit. Langkah pertama yang harus dilakukan:

1. Buat file dalam folder controller dengan nama Form_input.php

            class Form_input extends CI_Controller {

                        public function __construct(){

                                    parent::__construct();

                                    $this->load->helper('url_helper');

                        }

                        public function index (){

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

                                    $this->load->library('form_validation');

                                    $this->form_validation->set_rules('username','Username','required');

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

                                    $this->form_validation->set_rules('passconf','Confirm Password','required');

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

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

                                                $this->load->view('myform');

                                    }else{

                                                $this->load->view('form_success');

                                    }

                        }

            }

2. Setelah itu buat 2 file dalam folder views dengan nama myform.php dan form_success.php

  • myform.php

<div class="container">

                                    <div class="row">

                                                <div class="col-md-12">

                                                            <?php echo validation_errors(); ?>

                                                            <?php echo form_open('Form_input'); ?>

                                                                        <legend>Form Input</legend>

                                                                            <div class="form-group">

                                                                                    <label for="">Nama Lengkap</label>

                                                                                    <input type="text" class="form-control" id="" name="nama" placeholder="Input field">

                                                                        </div>

                                                                        <div class="form-group">

                                                                                    <label for="">Email</label>

                                                                                    <input type="text" class="form-control" name="email" id="" placeholder="Input field">

                                                                        <div class="form-group">

                                                                                    <label for="">Alamat</label>

                                                                                    <input type="text" class="form-control" name="alamat" id="" placeholder="Input field">

                                                                        </div>

                                                                <div class="form-group">

                                                                                    <label for="">Kota</label>

                                                                                    <input type="text" class="form-control" name="kota" id="" placeholder="Input field">

                                                                        </div>

              <input type="submit" value="KIRIM" class="btn btn-primary">

                                                            </form>

                                                </div>

                                    </div>

                        </div>

  • form_success.php

<div class="container">

                                    <div class="row">

                                                <div class="col-md-12">

                                                            <div class="alert alert-info">

                                                                        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>

                                                                        <strong>Success ! </strong> Data Berhasil ditambah ...

                                                            </div>

                                                            <p class="btn btn-warning"><?php echo anchor('Form_input', 'Try It Again!'); ?></p>

                                                </div>

                                    </div>

                        </div>

3. Jika Sudah simpan dan jalankan, maka hasilnya:

Download Tutorial