Window Models

Models are classes that help to communicate with database. The spoova Model enables the commnunication with database using database relationships. All ORM functionalities of the database model such as reading and deleting from database can be accessed from database relationships. This page will focus more on validating and inserting submitted request form data and since most of the Model class features discussed here are form-related, you can learn more about forms from here. The following are some of the methods of Model class and their functions some of which are also integerated with the Form class.

Model Methods
loadData
This method is used to load a submitted form data into a model class. An example of usage is shown below:
File - SampleModel.php

  namespace spoova\mi\window\Models;

  use Model;

  class SampleModel extends Model {

    function __construct() {

    }

  }
 

File - Home.php
  namespace windows\Routes;

  use spoova\mi\windows\Models\SampleModel;
  use spoova\mi\core\classes\Request;

  class Home extends Window {

    function __construct() {

        $Request =  new Request;
        $Model   =  new SampleModel;
      
        if( $Request->isPost() ) {
      
            $Model->loadData($Request->data()); //load form data into model class
      
        }
        
    }

  }
 

In the example above, once a post request is sent in Home url and the form data is submitted, the Home.php route class will load the data into the model class. The data can then be processed based on its required use.

loadedData
The method "loadedData()" is used to retrieve the data supplied into a model class through the laodData method.

rules
The method is used set a list of form rules which a model class must use to authenticate data supplied or loaded within it. Each data key to be validated must have a similar property name within the model class. The model will then try to validate each data key by using its corresponding property name. The rules method ensures that each form data key is validated through its list of validation rules defined for each key. These rules are discussed in Form rules.

validated
This method is used to initialize the data validation. It can only be called when two operations have be successfully satisfied. These operation includes loading of data and setting of data validation rules. In example "Home.php" above we can validate the data supplied in the following ways:
Home.php
 namespace windows\Routes;

 use spoova\mi\windows\Models\SampleModel;
 use spoova\mi\core\classes\Request;
 
 class Home extends Window {
 
   function __construct() {
 
       $Request =  new Request;
       $Model   =  new SampleModel;
     
       if( $Request->isPost() ) {
     
           $Model->loadData($Request->data()); 
           
           if($Model->validate()) {
             
             //data validated successfully
 
           }else {
             
             print_r($Model->errors());
             
           }
     
       }
       
   }
   
 }
 

In the above when the $Model->validate() is called, the entire loaded data will be validated. If all validation was successful, then the $Model->validate() method will return true else it will return false. If any error occurs, the error will be returned by the error() method.

error
The error() method as revealed in the previous example, returns the error messages encountered after validating each data key. The error returned on each data key is determined by the order of validation rules applied on the corresponding value of that key.

setError
The setError() method is used to set custom errors. The custom errors are usually stored under the array index key :mod. This method takes two arguments of key and value pairs where the first argument is the error name (or key) while the second argument is the error message. When we call the error() method, the custom messages can be obtained by first calling the array key :mod along with the defined message key. Using $Model->error() in the earlier code samples, we can obtain custom errors as shown below.
Home.php
 namespace windows\Routes;

 use spoova\mi\windows\Models\SampleModel;
 use spoova\mi\core\classes\Request;
 
 class Home extends Window {
 
   function __construct() {
 
       $Request =  new Request;
       $Model   =  new SampleModel;
     
       if( $Request->isPost() ) {
     
           $Model->loadData($Request->data()); 
           
           if($Model->validate()) {
             
             //data validated successfully
 
           }else {

             $Model->setError('someError', 'validation failed!');
             
             $modelErrors = $Model->errors();
             $customErrors = $modelErrors[':mod'] ?? [];

             var_dump($customErrors); // ['someError' => 'validation failed!'] 
             
           }
     
       }
       
   }
   
 }
 

Although, the example above may not be realistic enough, it simply explains how the setError and error works together

errorIndex
This method returns the first error message encountered after requested data's validation fails.

formdata
Since each request data contains a data key and its corresponding value, whenever a request data is loaded into the model class, the model will match the keys of data supplied with the model class property. Only keys having a similar property name will be returned as an array list by the formdata() method.

dataUpdate
A loaded data can be updated with the dataUpdate() method. This is usually done by supplying an array with the data key and value pairs of data keys that are needed to be updated.

tablename
The method tableName() is used to return a string of the database table name where database operations are expected to be performed.

Saved
The saved() method is usually used to save a data into a database table. It returns true if a data is succesfully saved into the database. This method is also capable of updating database values immediately before they are inserted into the database fields. Without any lasting modification of the real loaded data. In order to do this, the first argument must be an array of keys (column name) and value pairs.

isAuthenticated
This method by default returns true if data validation is successful and no errors are found.

mapform
The mapform() method is used to modify requested data column names. This is usually applicable in situations where form input data are submitted as the request data. At times, we may need to keep database table field names private because using the same form input name as database column names may not be advisable due to security issues. The mapform() method ensures that we can map database columns to html input forms. This method returns an array list of key and value pairs where the key is the form input's name and the value is the corresponding database field name. This means that value submitted into an html form input's field will be inserted into the relative column name defined.

As a conclusion, when it comes to the validation and authentication of request data, it is preferred to handle authentications through the Form class which is specially designed for form authentications through the use of form models. This class is also used to set custom error notices and also to separate form errors when working with multiple forms in a single web page.