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

Creating forms

Creating forms in MV has similar principles to creating models. You need to specify a list of fields, and then call methods to display and check the form data.

There are 2 approaches to creating a form:

  • Create a form based on a ready model.
  • Create a new form manually from the list of fields.

Form from model

Let's assume there is a Questions and Answers model, the site needs to organize the ability for visitors to leave questions, which will then be moderated in the admin panel.

class Faq extends Model
{
    protected $name = 'Questions and answers';
    
    protected $model_elements = [
        ['Active', 'bool', 'active'],
        ['Name', 'char', 'name', ['required' => true]],
        ['Date', 'date_time', 'date'],
        ['Question text', 'text', 'question', ['required' => true]],
        ['Answer text', 'text', 'answer']
    ];
    
    public function display()
    {
        ... //Displaying a list of questions and answers
    }
}

So, we have a ready-made model, some of whose fields need to be displayed on the site as a form. The fields we need are: 'Name' and 'Question text'. Let's assume that our form is displayed in the views/view-help.php template.

<?
$content = $mv -> pages -> find(['url' => 'help', 'active' => 1]);

$form = new Form('Faq');
$form -> useTokenCSRF();
?>
<h1><? echo $content -> name; ?></h1>
<form method='post'>
    <?
        echo $form -> display(['name', 'question']); 
        echo $form -> displayTokenCSRF();
    ?>
    <button>Submit</button>
</form>

Checking the correctness of the form fields and displaying a list of errors.

<?
$form = new Form('Faq');
$form -> useTokenCSRF();

$form -> submit() -> validate();
	
if($form -> isSubmitted() && $form -> isValid())
{
    ...
}
?>

<? echo $form -> displayErrors(); ?>
<form method='post'>
    ...
</form>

After checking the correctness of the fields, we need to send a new question to the database and reload the page to reset the POST data.

if($form -> isSubmitted() && $form -> isValid())
{
    $record = $mv -> faq -> getEmptyRecord();
    $record -> setValues($form -> all());
    $record -> date = I18n::getCurrentDateTime();

    $record -> create();
    $mv -> reload('?sent');
}

The form can also be built on the basis of a specific record from the model. Such a need arises, for example, to edit a record outside the admin panel. In this case, the second parameter when creating the form should be the id of the desired record from the model, and then the values โ€‹โ€‹โ€‹โ€‹of this record will be transferred to the form fields.

//Form from the model and a specific record
$form = new Form('Clients', 52);
$form -> loadRecord();

//You can pass an array of fields that need to be filled from the record data
$form -> loadRecord(['first_name', 'last_name', 'phone', 'email']);

Form without binding to a model

To create a form separate from the model, you need to specify a list of fields. The methods for working with such a form are similar to the previous example.

$fields = [
    ['Name', 'char', 'name', ['required' => true]],
    ['Email', 'email', 'email'],
    ['File', 'file', 'file', ['files_folder' => 'form_files']],
    ['Message', 'text', 'message', ['required' => true]]
];

$form = new Form($fields);
If the form is not built from a model and contains file fields, then there must be a parameter with the name of the folder for saving files files_folder, as in the example above.

If the form contains fields of the file, image, multi_images type, then you need to add JavaScript code to the page for buttons for deleting already uploaded files (it is assumed that the JQuery library is connected). This script allows you to delete the selected file and return the field for uploading a new file.

$(document).ready(function() 
{ 
    $('form .field-input span.delete').on('click', function() 
    { 
        $(this).parents('div.file-params').empty().next().show(); 
    }); 
}); 
Similar code in pure JavaScript can be found in the MV starter build in the media/js/intro.js file.

Previous section

Special methods

Next section

Setting up form fields
MV workshop banner
MV tracker

© 2014-2025, MV framework team

MV tracker project Github