MV framework logo
  • Architecture
    • Philosophy
    • Technologies
  • Admin Panel
  • Changelog
  • Support
  • Feedback
Download Version 2.6 from 07.10.2022
Documentation
  • Architecture
    • Philosophy
    • Technologies
  • Admin Panel
  • Changelog
  • Support
  • Feedback
Getting started
  • Installation and launch
  • System folders
  • Launch of simple website
  • Pre-installed models
  • System settings
Models
  • General principles of models
  • Data types
  • Model settings
  • Simple models
  • Foreign keys
  • Trees
  • Many to many
  • Group
  • Records management
  • Simple model management
  • Additional options
Views and routes
  • General principles of views
  • Router object
  • MV object
  • Index, default and 404 templates
  • Creating a new template
  • Data display in template
  • Object of Record class
  • Files and images
  • Date and time
  • Redirects
  • Email delivery
  • Special methods
Forms
  • Forms creation
  • Form fields settings
  • Form validation rules
  • Form validation process
  • Operations with form data
  • Using data from models
  • Form methods
SQL queries
  • Query constructor
  • Direct queries
  • Pagination
  • Sorting
  • Filtration
  • Upload from CSV files
Sessions and security
  • Work with sessions
  • AJAX
  • Security
  • Debugging
Plugins
  • General principles of plugins
  • Additions to admin panel
  • Caching
Documentation
Getting started
  • Installation and launch
  • System folders
  • Launch of simple website
  • Pre-installed models
  • System settings
Models
  • General principles of models
  • Data types
  • Model settings
  • Simple models
  • Foreign keys
  • Trees
  • Many to many
  • Group
  • Records management
  • Simple model management
  • Additional options
Views and routes
  • General principles of views
  • Router object
  • MV object
  • Index, default and 404 templates
  • Creating a new template
  • Data display in template
  • Object of Record class
  • Files and images
  • Date and time
  • Redirects
  • Email delivery
  • Special methods
Forms
  • Forms creation
  • Form fields settings
  • Form validation rules
  • Form validation process
  • Operations with form data
  • Using data from models
  • Form methods
SQL queries
  • Query constructor
  • Direct queries
  • Pagination
  • Sorting
  • Filtration
  • Upload from CSV files
Sessions and security
  • Work with sessions
  • AJAX
  • Security
  • Debugging
Plugins
  • General principles of plugins
  • Additions to admin panel
  • Caching

Form validation process

<?
$fields = array(array("Organization", "char", "company", array("required" => true)),
                array("Number of employees", "int", "staff"),   
                array("Business type", "enum", "type", array("values_list" => array("trade" => "Trade",
                                                                                    "law" => "Law",
                                                                                    "manufacturing" => "Manufacturing")),
                array("Email", "email", "email", array("required" => true)),
                array("Password to enter ", "password", "password", array("required" => true, 
                                                                          "min_length" => 10, 
                                                                          "letters_required" => true)),
                array("Activity description", "text", "about", array("required" => true))
);

$form = new Form($fields);
?>

Validation of all form fields

Before data validation you need to fill form field values, using "getDataFromArray()" or "getDataFromPost()" method. Fields of the form are validated by "validate()" method which reviews all the fields and checks them to meet requirements (mandatory fields, an integer, e-mail format etc). If during verification errors occurred, they will be stored in the object of the form to display them in template.

if(!empty($_POST))
{    
    $form -> getDataFromPost() -> validate();

    //Get fields values and their validation (all form fields)
    if(!$form -> isValid()) //If a form contains errors 
    {
        ...
    }
    else
    {
        //No errors, do required actions  
        //(creation / editing of record, send e-mail message etc)

        ...

    }
}

//Then in html part of a template display list of errors in a needed place
echo $form -> displayErrors();

//Also we may display errors near fields
echo $form -> setDisplayWithErrors() -> display();

Validation of selected form fields

Very often we have a situation when we only need to validate several fields of the form, for this case an array of field names will be passed for validation.

if(!empty($_POST))
{
    $form -> getDataFromPost() -> validate(array("company", "email"));
}

Additional field validation and adding an error

$value = $form -> getValue("about");

//Or like this
$value = $form -> about;

if(strlen($value) < 100)
    $form -> addError("Please, enter a more detailed description of your activities.");

//If form errors are shown near fields (after setDisplayWithErrors() call), 
//then we need to bind an additional error to the specified field,
//by passing the name of field as a second parameter

$form -> addError("Please, enter a more detailed description of your activities.", "about");

Set a field value individually

$form -> setValue("about", "Description of activites is not entered.");

//Or like this
$form -> about = "Description of activites is not entered.";

Previous section

Form validation rules

Next section

Operations with form data

© 2012-2023, MV framework

MV tracker is based on open source MV framework
License