Skip to content

Commit 630144f

Browse files
committed
init
1 parent c709054 commit 630144f

File tree

5 files changed

+218
-1
lines changed

5 files changed

+218
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 izica
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,75 @@
1-
# phalcon-validation
1+
# Phalcon validation
2+
23
Handy interface for Phalcon\Validation
4+
5+
### Installation
6+
```
7+
composer require izica/phalcon-validation
8+
```
9+
10+
### Api
11+
Izica\Validation
12+
* __construct($arOptions) // example below
13+
* validate($arData)
14+
* static required
15+
* static numeric
16+
* static email
17+
* TODO ----
18+
* length
19+
* callback
20+
* etc
21+
22+
### Usage
23+
```php
24+
use Izica\Validation;
25+
use Phalcon\Mvc\Controller;
26+
27+
class ExampleController extends Controller {
28+
public function indexAction() {
29+
$validation = new Validation([
30+
'email' => [Validation::required(), Validation::email()],
31+
'num' => [Validation::required(), Validation::numeric()],
32+
]);
33+
$arMessages = $validation->validate($_POST);
34+
if ($arMessages) {
35+
// validation error
36+
}
37+
// validation success
38+
}
39+
}
40+
```
41+
42+
```
43+
$arMessages = $validation->validate($_POST);
44+
45+
Request
46+
email: qwe
47+
num:
48+
49+
Response:
50+
Array
51+
(
52+
[0] => Array
53+
(
54+
[type] => email
55+
[message] => email is not valid
56+
[field] => email
57+
)
58+
59+
[1] => Array
60+
(
61+
[type] => num
62+
[message] => num is required
63+
[field] => num
64+
)
65+
66+
[2] => Array
67+
(
68+
[type] => num
69+
[message] => num is not numeric
70+
[field] => num
71+
)
72+
)
73+
74+
```
75+

Validation.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace Izica;
4+
5+
use Phalcon\Validation as PhalconValidation;
6+
use Phalcon\Validation\Validator\Email;
7+
use Phalcon\Validation\Validator\PresenceOf;
8+
use Phalcon\Validation\Validator\Numericality;
9+
10+
class Validation {
11+
private static $sTypeRequired = 'required';
12+
private static $sTypeNumeric = 'numeric';
13+
private static $sTypeEmail = 'email';
14+
private $arOptions = [];
15+
public static $arValidators = [
16+
'required' => PresenceOf::class,
17+
'numeric' => Numericality::class,
18+
'email' => Email::class,
19+
];
20+
21+
function __construct($arOptions) {
22+
$this->arOptions = $arOptions;
23+
}
24+
25+
private static function getValidator($sName, $arOptions) {
26+
if (self::$arValidators[$sName]) {
27+
return new self::$arValidators[$sName]($arOptions);
28+
}
29+
return false;
30+
}
31+
32+
public function validate($arData, $bKeys = true) {
33+
$obValidation = new PhalconValidation();
34+
foreach ($this->arOptions as $sField => $arFieldOptions) {
35+
foreach ($arFieldOptions as $arFieldOption) {
36+
$obValidator = self::getValidator($arFieldOption['type'], $arFieldOption['options']);
37+
if ($obValidator !== false) {
38+
$obValidation->add($sField, $obValidator);
39+
}
40+
}
41+
}
42+
$obMessages = $obValidation->validate($arData);
43+
$arMessages = [];
44+
if ($bKeys) {
45+
foreach ($obMessages as $obMessage) {
46+
$arMessages[$obMessage->getField()][$obMessage->getType()] = $obMessage->getMessage();
47+
}
48+
}else{
49+
foreach ($obMessages as $obMessage) {
50+
$arMessages[] = $obMessage->getMessage();
51+
}
52+
}
53+
return $arMessages;
54+
}
55+
56+
public static function required(
57+
$arOptions = [
58+
'message' => ':field is required'
59+
]
60+
) {
61+
return [
62+
'type' => self::$sTypeRequired,
63+
'options' => $arOptions
64+
];
65+
}
66+
67+
public static function numeric(
68+
$arOptions = [
69+
'message' => ':field is not numeric'
70+
]
71+
) {
72+
return [
73+
'type' => self::$sTypeNumeric,
74+
'options' => $arOptions
75+
];
76+
}
77+
78+
public static function email(
79+
$arOptions = [
80+
'message' => ':field is not valid'
81+
]
82+
) {
83+
return [
84+
'type' => self::$sTypeEmail,
85+
'options' => $arOptions
86+
];
87+
}
88+
}

composer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "izica/phalcon-validation",
3+
"description": "Handy interface for Phalcon Validation",
4+
"authors": [
5+
{
6+
"name": "Golovarchuk Artyom",
7+
"email": "[email protected]"
8+
}
9+
],
10+
"keywords": [
11+
"validation",
12+
"validator",
13+
"validate",
14+
"validation-library",
15+
"validations",
16+
"validators",
17+
"phalcon",
18+
"phalcon-php",
19+
"phalcon-framework",
20+
"interface",
21+
"php",
22+
"php7",
23+
"phalcon-php-framework"
24+
],
25+
"license": "MIT",
26+
"require": {
27+
"php": ">=5.6.0"
28+
},
29+
"autoload": {
30+
"classmap": [
31+
"Validation.php"
32+
]
33+
}
34+
}

0 commit comments

Comments
 (0)