Validators
Validators objects perform sanity checks on the user input. Validators can be added to a web form statically (mapped in the data manager class) or at runtime.
The following validators are built-in the framework:
* miValidatorDate - checks if the data is a valid date string
* miValidatorDecimal - ensures that data is valid decimal number
* miValidatorEmail - check if the data is valid e-mail address
* miValidatorGsm - checks if the data is valid mobile phone number
* miValidatorHttp - ensures that the data is valid www location
* miValidatorIcq - checks if the data is valid ICQ number
* miValidatorInt - ensures that data is integer
* miValidatorIp - ensures that data is an computer IP (IPv4)
* miValidatorUnique - ensures that the value is unique in the database column
For text fields 'minLength' and 'maxLength' properties are available to check if field data is the correct length.
* Map validators
<?php
class miUserDataManager extends miDataManager
{
protected $_dataFields = array(
array(
'field' => 'miWebFormWidgetText',
'data' => 'UserEmail',
'validator' => array('miValidatorEmail')
)
);
}
?>
* Create validators runtime
<?php
class miUserDataManager extends miDataManager
{
protected $_dataFields = array(
array(
'field' => 'miWebFormWidgetText',
'data' => 'UserLoginName'
),
array(
'field' => 'miWebFormWidgetText',
'data' => 'UserICQ'
)
);
public function initWebForm(miWebForm $form)
{
parent::initWebForm($form);
$record = new miSqlRecord('Users', 'UserID');
// Unique validator can be created only at runtime because requires additional parameters
$loginFieldValidator = new miValidatorUnique($form, 'UserLoginName', $record);
$form->addValidator('UserLoginName', $loginFieldValidator);
$icqFieldValidator = new miValidatorIcq($form, 'UserICQ');
$form->addValidator('UserICQ', $icqFieldValidator);
}
}
?>
* Usage of minLength and maxLength properties
<?php
// In this example the 'UserPassword' field allows passwords from 4 to 32 characters (inclusive)
class miUserDataManager extends miDataManager
{
protected $_dataFields = array(
array(
'field' => 'miWebFormWidgetText',
'data' => 'UserPassword',
'properties' => array('minLength' => 4, 'maxLength' => 32)
)
);
}
?>