Thursday, 24 July 2014

Data Validation Techniques In CakePHP

In the previous tutorial author, entitled CRUD Database Using CakePHP there is an important point that the author forgot to include in the tutorial, to make amends, the writers would complement these deficiencies in our tutorial this time.

Important point that has not been touched in the previous tutorial is a data validation when we will do the inputting of data. Actually what is the usefulness of the validation? Validation we use to prevent or gives an error message to the user, when the user did not input data according to certain data formats, for example, users do not forget to fill in one of the data fields, data type should figure apparently entered by the user in the form of a user. Well from the few examples above, then we need to prevent it by making validation.

In the CakePHP is equipped with a built-in feature validation, where validation is formed on a model.

Imagine that we are not confused theory that the writer explained above, we will practice only makes the data validation on Database CRUD application in the previous article. Where are we going to change a few models that we have made is that we keep Employee.php file in the folder \ htdocs \ crud-cake \ app \ model.

We change a few files Employee.php be as follows

<?php
class Employee extends AppModel {
  public $validate = array (
     'nip'=> array (
           /nama rule yang kita pakai adalah numeric
          * agar NIP yang diinputkan hanya berisi angka saja/
          'rule'=>'numeric',
          /pesan error yang ditampilkan 
             * jika user tidak menginputkan NIP dengan angka/
         'message'=>'NIP Harus angka'
        ),
     
       'nama'=> array (
          /nama rule yang kita pakai adalah minLength
            * agar nama yang diinputkan minimal adalah 3 huruf/
          'rule'=>array('minLength', '3'),
          //data pada nama tidak boleh kosong
            'required'=> true,
            /pesan error yang ditampilkan 
             * jika user tidak menginputkan nama minimal 3 huruf atau tidak menginputkan data/
            'message'=>'Nama tidak boleh kosong dan minimal 3 huruf'
        ),
     
       'golongan'=> array (
          /nama rule yang kita pakai adalah notEmpty
             * agar golongan yang diinputkan tidak boleh kosong/
          'rule'=> 'notEmpty'
     ),
     'pangkat'=> array (
           'rule'=> 'notEmpty,'
        ),
     
   );
}
?>

in addition to the name of a numeric rule, minLength, there notEmpty rule name that is often used, among other things:

     email, are used to validate the format of email
     MaxLength, the logic function is similar to minLength, MaxLength to validate the difference is the maximum number of characters entered number
     TransCanada, are used to validate a certain number of characters required, eg 2 min and max number of characters number of characters 10
     alphanumeric, is used to validate user input character to be a combination of letters and numbers (for example, when we make a password)