Wednesday 29 July 2015

FORM VALIDATION SERVER SIDE DENGAN CODEIGNITER


Validasi penting untuk keamanan website dengan form yang kompleks. Dari sisi client side maupun server side. Disini gue akan menjelaskan sedikit wawasan gue tentang validasi menggunakan Form Validation bawaan CI.
Kata bapak gue :
Sesuatu yang lebih aman memang rumit, tapi akan lebih rumit ketika keamananmu diambil alih orang tak bertanggung jawab
Dari situ dapat diambil filosofinya bro, merawat lebih baik dari mengobati. :) *nyambung gak?*
Pertama, lakukan load library dengan kode seperti berikut :
1
2
3
4
function index()
{
    $this->load->library('form_validation');
}
Validasi digunakan sesuai nama (name) input dari form. Penggunaannya perlu kode set_rules()
1
$this->form_validation->set_rules('uname', 'Username', 'required|min_length[4]|max_length[10]);
Ada 3 parameter
  1. Nama field.
  2. Nama yang dapat ditampilkan.
  3. Rule yang akan dipakai.
1
$this->form_validation->set_rules('uname', 'Username', 'required');
Jika rule lebih dari satu
1
$this->form_validation->set_rules('uname', 'Username', 'required|min_length[4]|max_length[10]);
Keluaran dari fungsi ini adalah true sama false bro.
Ini list rule punya CI :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
required // Returns FALSE if the form field is empty.
 
matches // Returns FALSE if the form field does not match the defined value of parameter.
 
is_unique // Returns FALSE if the form field is not unique to the table and field name in the parameter.
 
min_length // Returns FALSE if the form field is shorter than the parameter value.
 
max_length // Returns FALSE if the form field is longer than the parameter value.
 
exact_length // Returns FALSE if the form field is not exactly the parameter value.
 
greater_than // Returns FALSE if the form field is less than the parameter value or not numeric.
 
less_than // Returns FALSE if the form field is greater than the parameter value or not numeric.
 
alpha // Returns FALSE if the form field contains anything other than alphabetical characters.
 
alpha_numeric // Returns FALSE if the form field contains anything other than alpha-numeric characters.
 
alpha_dash // Returns FALSE if the field contains anything other than alpha-numeric characters, underscores or dashes.
 
numeric // Returns FALSE if the form field contains anything other than numeric characters.
 
integer // Returns FALSE if the form field contains anything other than an integer.
 
decimal // Returns FALSE if the form field contains anything other than a decimal number.
 
is_natural // Returns FALSE if the form field contains anything other than a natural number.
 
is_natural_no_zero // Returns FALSE if the form field contains anything other than a natural number, but not zero.
 
valid_email // Returns FALSE if the form field does not contain a valid email address.
 
valid_emails // Returns FALSE if any value provided in a comma separated list is not a valid email.
 
valid_ip // Returns FALSE if the supplied IP is not valid.
 
valid_base64 // Returns FALSE if the supplied string contains anything other than valid Base64 characters

Artikel Terkait

FORM VALIDATION SERVER SIDE DENGAN CODEIGNITER
4/ 5
Oleh

Berlangganan

Suka dengan artikel di atas? Silakan berlangganan gratis via email

Coretan Disqus

close
close