Useful tool for Phalcon\Validation for easiest way to validate your data
composer require izica/phalcon-validation
Izica\Validation
__construct($arOptions)
-- example belowvalidate($arData)
static make($arData, $arOptions)
Izica\ValidationRule
static required($arOptions: optional)
-- Phalcon\Validation\Validator\PresenceOfstatic numeric($arOptions: optional)
-- Phalcon\Validation\Validator\Numericalitystatic email($arOptions: optional)
-- Phalcon\Validation\Validator\Emailstatic url($arOptions: optional)
-- Phalcon\Validation\Validator\Urlstatic callback($arOptions)
-- Phalcon\Validation\Validator\Callbackstatic length($arOptions)
-- Phalcon\Validation\Validator\StringLengthstatic unique($arOptions)
-- Phalcon\Validation\Validator\UniquenessValidatorstatic between($arOptions)
-- Phalcon\Validation\Validator\Betweenstatic file($arOptions)
-- Phalcon\Validation\Validator\Filestatic date($arOptions)
-- Phalcon\Validation\Validator\Datestatic regex($arOptions)
-- Phalcon\Validation\Validator\Regex
[https://docs.phalconphp.com/3.4/en/api/phalcon_validation_validator_numericality]
$arOptions
which passed in static functions, for example static numeric($arOptions)
,
used as params for new Numericality($arOptions)
;
You can use it like this
$validation = new Validation([
'num' => [ValidationRule::numeric(['message' => ':field is not numeric')],
]);
use Izica\Validation;
use Izica\ValidationRule;
use Phalcon\Mvc\Controller;
class ExampleController extends Controller {
public function indexAction() {
$validation = new Validation([
'email' => [ValidationRule::required(), ValidationRule::email()],
'num' => [ValidationRule::required(), ValidationRule::numeric()],
]);
$arMessages = $validation->validate($_POST);
if ($arMessages) {
// validation error
}
// validation success
}
// or short
public function indexAction() {
$arMessages = Validation::make($_POST, [
'email' => [ValidationRule::required(), ValidationRule::email()],
'num' => [ValidationRule::required(), ValidationRule::numeric()],
]);
if ($arMessages) {
// validation error
}
// validation success
}
}
/*
$_POST = [
'email' => 'qwe'
];
*/
$arMessages = $validation->validate($_POST);
/*
$arMessages
Array
(
[0] => Array
(
[field] => email
[type] => email
[message] => email is not valid
)
[1] => Array
(
[field] => num
[type] => required
[message] => num is required
)
[2] => Array
(
[field] => num
[type] => numeric
[message] => num is not numeric
)
)
*/