Using data from models

Form values from Record object

When we create a form from a model it's possible to pass the form values from a database for their further validation and change. This option is applied when editing data on frontend, for example in private rooms of users on the website.

class Clients extends Model 
{
    protected $name = "Clients";

    protected $model_elements = array(
        array("Name", "char", "name", array("required" => true)),
        array("E-mail", "email", "email", array("required" => true, "unique" => true)),
        array("Password", "password", "password", array("required" => true)),
        array("City", "enum", "city", array("foreign_key" => "Cities"))
    ):
}

$client = $mv -> clients -> findRecordById(93);

//Pass an id of record into a form
$form = new Form("Clients", $client -> id);

//Load data into a form
$form -> loadRecord();
 
//We may load only selected fields  
$fields = array("name", "city"); 
$form -> loadRecord($fields); 
 
echo $form -> display();

Filtration of foreign key values

The form built on one model can use data from other model (with the help of foreign key). Such situations are possible when not all variants of foreign keys need to be shown in the form. Parameters of filtering are described in the Query constructor section.

Example of the user registration form (the models from previous example) with a city selection.

class Cities extends Model
{
   protected $name = "Cities";
 
   protected $model_elements = array(
       array("Activate","bool", "active", array("on_create" => true)),
       array("Name", "char", "name", array("required" => true)),
       array("Country", "enum", "country", array("foreign_key" => "Countries"))
   );
}
 
//In a template file show filtered cities in a form 
$form = new Form("Clients");
$form -> filterValuesList("city", array("active" => 1, "order->asc" => "name"));

echo $form -> display();