MV framework logo
  • Architecture
  • Philosophy
  • Admin panel
  • Support
  • Feedback
Documentation
Download .zip version 3.2.0 from 25.12.2024
  • Architecture
  • Philosophy
  • Admin panel
  • Support
  • Feedback
Getting started
  • Installation and launch
  • Folder structure
  • Launching a simple website
  • Pre-installed models
  • SQLite getting started
  • System settings
  • Migrations
  • Debugging
Models
  • General principles of models
  • Data types
  • Model setup
  • Simple models
  • Foreign keys
  • Trees
  • Many to many
  • Group
  • Records management
  • Managing simple models
  • Additional features
Templates and routing
  • General principles of templates
  • Router object
  • MV object
  • Create a new template
  • Output of data in template
  • Record object
  • Files and images
  • Date and time
  • Redirects and http
  • Sending email
  • Special methods
Forms
  • Creating forms
  • Setting up form fields
  • Validating form fields
  • Form security
  • Working with form data
  • Using data from models
  • Form methods
SQL queries
  • Query builder
  • Direct queries
  • Pagination
  • Sorting
  • Filtration
Additional
  • AJAX
  • Plugins
  • Caching
  • Security
  • Admin panel add-ons
Documentation
Getting started
  • Installation and launch
  • Folder structure
  • Launching a simple website
  • Pre-installed models
  • SQLite getting started
  • System settings
  • Migrations
  • Debugging
Models
  • General principles of models
  • Data types
  • Model setup
  • Simple models
  • Foreign keys
  • Trees
  • Many to many
  • Group
  • Records management
  • Managing simple models
  • Additional features
Templates and routing
  • General principles of templates
  • Router object
  • MV object
  • Create a new template
  • Output of data in template
  • Record object
  • Files and images
  • Date and time
  • Redirects and http
  • Sending email
  • Special methods
Forms
  • Creating forms
  • Setting up form fields
  • Validating form fields
  • Form security
  • Working with form data
  • Using data from models
  • Form methods
SQL queries
  • Query builder
  • Direct queries
  • Pagination
  • Sorting
  • Filtration
Additional
  • AJAX
  • Plugins
  • Caching
  • Security
  • Admin panel add-ons
MV tracker

Using data from models

Loading a record into a form

When creating a form from a model, you can immediately load values ​​from the database into the form for further editing. This feature is used, for example, when editing data in personal accounts, users on the site.

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

    protected $model_elements = [
        ['Name', 'char', 'name', ['required' => true]], 
        ['E-mail', 'email', 'email', ['required' => true, 'unique' => true]], 
        ['Password', 'password', 'password', ['required' => true]], 
        ['City', 'enum', 'city', ['foreign_key' => 'Cities', 'empty_value' => true]] 
    ];
}

$client = $mv -> clients -> find(93); 
$form = new Form('Clients', $client -> id);

//Loading data into the form 
$form -> loadRecord();

//Only selected fields can be loaded 
$form -> loadRecord(['name', 'city']); 
echo $form -> display(); 

Foreign key filtering

A form built on one model can use data from another model in enum and many_to_many fields. There may be situations when not all foreign key options need to be shown in the form and they need to be filtered or sorted. Filtering parameters are described in the Query builder section.

An example of a user registration form with a city selection.

//Model class for the 'city' field of the client

class Cities extends Model
{
    protected $name = 'Cities';
    
    protected $model_elements = [
        ['Activate','bool', 'active', ['on_create' => true]],
        ['Name', 'char', 'name', ['required' => true]],
        ['Country', 'enum', 'country', ['foreign_key' => 'Countries']]
    ];
}

//In the template file, we show the filtered cities in the form
$form = new Form('Clients');
$form -> filterValuesList('city', ['active' => 1, 'order->asc' => 'name']);
echo $form -> display();

Previous section

Working with form data

Next section

Form methods
MV workshop banner
MV tracker

© 2014-2025, MV framework team

MV tracker project Github