michele-angioni / phalcon-validators
New advanced validators for PHP Phalcon Framework.
Installs: 3 883
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 6
Forks: 1
Open Issues: 0
Requires
- php: >=7.1
- ext-phalcon: >=3.1.2
Requires (Dev)
- phalcon/incubator: dev-master
- phpunit/phpunit: ^5.7
README
Introduction
Phalcon Validators adds several new validators to the few default ones present in Phalcon.
PHP 7.1+ and Phalcon 3.1 are required.
Installation
Support can be installed through Composer, just include "michele-angioni/phalcon-validators": "~2.0"
to your composer.json and run composer update
or composer install
.
Usage
The new validators work in the same way the default validators do.
Just pass a new instance of the validator to the Phalcon Validation
class, with the desired options, and then validate it.
Available validators with practical examples:
IpValidator
The IpValidator validates a valid ip address.
$data['ip'] = $this->request->getPost('ip'); $validation = new Phalcon\Validation(); $validation->add( 'ip', new MicheleAngioni\PhalconValidators\IpValidator ( [ 'message' => 'The IP is not valid.' // Optional ] ) ); $messages = $validation->validate($data); if (count($messages)) { // Some error occurred, handle messages }
// Validation succeeded without errors
NumericValidator
The default NumericValidator only allows for numeric (i.e. 0-9) characters. Minimum and maximum values can be specified.
Optionally, it can support float values, that is allowing a dot (.) character to separate decimals.
Optionally also signed numbers are supported.
$data['number'] = $this->request->getPost('number'); $validation = new Phalcon\Validation(); $validation->add( 'number', new MicheleAngioni\PhalconValidators\NumericValidator ( [ 'allowFloat' => true, // Optional, default: false 'allowSign' => true, // Optional, default: false 'min' => 2, // Optional 'min' => 2, // Optional 'max' => 50, // Optional 'message' => 'Only numeric (0-9,.) characters are allowed.', // Optional 'messageMinimum' => 'The value must be at least 2', // Optional 'messageMaximum' => 'The value must be lower than 50' // Optional ] ) ); $messages = $validation->validate($data); if (count($messages)) { // Some error occurred, handle messages } // Validation succeeded without errors
AlphaNumericValidator
The AlphaNumericValidator allows for alphanumeric characters. Optionally, it can allow underscores, minuses and white spaces. Minimum and maximum string lengths can be specified.
$data['text'] = $this->request->getPost('text'); $validation = new Phalcon\Validation(); $validation->add( 'text', new MicheleAngioni\PhalconValidators\AlphaNumericValidator ( [ 'whiteSpace' => true, // Optional, default false 'underscore' => true, // Optional, default false 'minus' => true, // Optional, default false 'min' => 6, // Optional 'max' => 30, // Optional 'message' => 'Validation failed.', // Optional 'messageMinimum' => 'The value must contain at least 6 characters.', // Optional 'messageMaximum' => 'The value can contain maximum 30 characters.' // Optional ] ) ); $messages = $validation->validate($data); if (count($messages)) { // Some error occurred, handle messages } // Validation succeeded without errors
AlphaNamesValidator
The AlphaNamesValidator allows for alphabetic, menus, apostrophe, underscore and white space characters. Optionally, it can allow also numbers (i.t. 0-9). Minimum and maximum string lengths can be specified.
$data['text'] = $this->request->getPost('text'); $validation = new Phalcon\Validation(); $validation->add( 'text', new MicheleAngioni\PhalconValidators\AlphaNamesValidator ( [ 'numbers' => true, // Optional, default false 'min' => 6, // Optional 'max' => 30, // Optional 'message' => 'Validation failed.', // Optional 'messageMinimum' => 'The value must contain at least 6 characters.', // Optional 'messageMaximum' => 'The value can contain maximum 30 characters.' // Optional ] ) ); $messages = $validation->validate($data); if (count($messages)) { // Some error occurred, handle messages } // Validation succeeded without errors
AlphaCompleteValidator
The AlphaCompleteValidator allows for alphanumeric, underscore, white space, slash, apostrophe, round and square brackets/parentheses and punctuation characters. Optionally, it can allow also pipes (|), ATs (@), backslashes (), percentages (%) and Url Characters (equals (=) and hashtags (#)). Minimum and maximum string lengths can be specified.
$data['text'] = $this->request->getPost('text'); $validation = new Phalcon\Validation(); $validation->add( 'text', new MicheleAngioni\PhalconValidators\AlphaCompleteValidator ( [ 'allowBackslashes' => true, // Optional 'allowAt' => true, // Optional 'allowPipes' => true, // Optional 'allowPercentages' => true, // Optional 'allowUrlChars' => true, // Optional 'min' => 6, // Optional 'max' => 30, // Optional 'message' => 'Validation failed.', // Optional 'messageMinimum' => 'The value must contain at least 6 characters.', // Optional 'messageMaximum' => 'The value can contain maximum 30 characters.' // Optional ] ) ); $messages = $validation->validate($data); if (count($messages)) { // Some error occurred, handle messages } // Validation succeeded without errors
FileNameValidator
The FileNameValidator allows for a valid file name with extension, allowing simple letters, numbers underscores and minuses. Optionally, it can allow also all Latin characters, multiple dots and white spaces. Minimum and maximum string lengths can be specified.
$data['text'] = $this->request->getPost('text'); $validation = new Phalcon\Validation(); $validation->add( 'text', new MicheleAngioni\PhalconValidators\FileNameValidator ( [ 'allowMultipleDots' => true, // Optional 'allowAllLatin' => true, // Optional 'allowSpaces' => true, // Optional 'min' => 6, // Optional 'max' => 30, // Optional 'message' => 'Validation failed.', // Optional 'messageMinimum' => 'The value must contain at least 6 characters.', // Optional 'messageMaximum' => 'The value can contain maximum 30 characters.' // Optional ] ) ); $messages = $validation->validate($data); if (count($messages)) { // Some error occurred, handle messages } // Validation succeeded without errors
Contribution guidelines
Phalcon Validators follows PSR-1, PSR-2 and PSR-4 PHP coding standards, and semantic versioning.
Pull requests are welcome.
License
Phalcon Validators is free software distributed under the terms of the MIT license.