Validating Input Data with the CodeIgniter PHP Framework - Review: the complete source code of the generic model class
(Page 2 of 4 )
Before I proceed to implement a method within the generic model class that performs customized validation on the data used for inserting and updating database records, I’m going to list the model’s entire source code. This way, you’ll be able to recall more quickly how it was defined in the previous article of the series.
Having clarified that point, here’s the complete signature of the “AbstractModel” class, which now has the ability to perform CRUD operations against a specified database table. Have a look at it:
abstract class MY_Model extends Model
{
protected $table = ''; // table associated to the model
protected $fields = array(); // fields of table associated to the model
protected $id = NULL; // value of the primary key of the table associated to the model
protected $data = array(); // model input data
protected $insertID = NULL; // insertion ID
protected $numRows = NULL; // number of rows returned by SELECTS
protected $validation = array(); // model validation rules
As you’ll possibly recall after analyzing the source code above, at this point the model is capable of retrieving, inserting, updating and deleting database records by means of the respective “fetch(),” “save()” and “delete()” methods, which admittedly are pretty intuitive. Nonetheless, as you may have noticed, the “save()” method checks the validity of its incoming data via another protected function, not surprisingly called “validate().”
You're probably wondering how this method looks. Well, it’s hasn’t been defined yet, but don’t worry; in the following section, I’m going to show you its implementation, thus dissipating any doubts that you may have regarding its functionality.
Thus, to learn how the private “validate()” method will be coded, click on the link that appears below and read the following segment.