Skip to content

Commit 4bd9f9b

Browse files
committed
PHP Validator Class v1.0
0 parents  commit 4bd9f9b

File tree

4 files changed

+440
-0
lines changed

4 files changed

+440
-0
lines changed

Diff for: LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Kunal Ali Khan
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.

Diff for: README.md

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# PHP Validator Class `v1.0`
2+
This is a simple PHP Validator Class. It requires no dependencies. Fork it and have fun with it.
3+
4+
It's a standalone single file PHP class to use on your projects. It requires no dependencies or no framework.
5+
6+
## How To Use
7+
You only need only one file:
8+
9+
- `Validator.php`
10+
11+
You can run everything from the index.php file, see the file for usage.
12+
13+
### Step 1 - Initialize
14+
Before start
15+
```php
16+
// Include the class file
17+
require "Validator.php";
18+
19+
// Data to validate
20+
$data = [
21+
"name" => "John Doe",
22+
"age" => 25,
23+
"email" => "[email protected]",
24+
"password" => "pass@123",
25+
"confirm_password" => "pass@123",
26+
"sex" => "male",
27+
"phone" => "1236547895"
28+
];
29+
```
30+
31+
Create the class instance
32+
```php
33+
$v = new Validator($data);
34+
```
35+
Use a associative array as data
36+
37+
### Step 2 -
38+
Run validation by chaining methods to the `field()` method. [Check here](#methods) to see description of every methods.
39+
```php
40+
$v->field('name')->required()->alpha([' ']);
41+
$v->field('age') ->required()->numeric()->min_val(14)->max_val(100);
42+
$v->field('email')->required()->email();
43+
$v->field('password')->required()->min_len(8)->max_len(16)->must_contain('@#$&')->must_contain('a-z')->must_contain('A-Z')->must_contain('0-9');
44+
$v->field('confirm_password')->required()->equals($data['password']);
45+
$v->field('sex')->enum(['male', 'female', 'others']);
46+
$v->field('phone')->numeric()->min_len(10)->max_len(10);
47+
```
48+
Make sure to run the field method on start of every method chain.
49+
50+
### Step 3 -
51+
Check if data is valid
52+
```php
53+
if(!$v->is_valid()){
54+
// Print the error messages
55+
print_r($v->error_messages);
56+
}
57+
```
58+
59+
## Properties
60+
- `array $error_messages` - Get the list of generated error messages.
61+
62+
## Methods
63+
Some methods to use
64+
| Methods | Return | Description |
65+
|--------|--------|-------------|
66+
| `set_response_messages(arr $messages)` | void | Function to set/extend custom error. <br /> Use associative array of messages as the parameter. See the messages format on `Validator.php` file at line `20`.
67+
| is_valid() | boolean | Check if all validations are successfull.
68+
69+
Here is a list of the validators currently available.
70+
71+
| Validator | Description |
72+
| ----------|-------------|
73+
| `required()` | Check if the value exists. |
74+
| `alpha(arr $ignore)` | Check if the value is alpha only. <br/> param *array* `$ignore` - (optional) add charectors to allow. Ex. ['@', ' '] |
75+
| `alpha_num()` | Check if the value is alpha numeric only. <br/> param *array* `$ignore` - (optional) add charectors to allow. Ex. ['@', ' '] |
76+
| `numeric()` | Check if the value is numeric only. |
77+
| `email()` | Check if the value is a valid email. |
78+
| `max_len(int $size)` | Check if length of the value is larger than the limit. <br/> param *int* `$size` - Max length of charectors of the value. |
79+
| `min_len(int $size)` | Check if length of the value is smaller than the limit. <br/> param *int* `$size` - Min length of charectors of the value. |
80+
| `max_val(int $val)` | Check if the value of intiger/number is not larger than the limit. <br/> param *int* `$val` - Max value of the number. |
81+
| `min_val(int $val)` | Check if the value of intiger/number is not smaller than the limit. <br/> param *int* `$val` - Min value of the number. |
82+
| `enum(arr $list)` | Check if the value is in the list. <br/> param *array* `$list` - List of valid values. |
83+
| `equals(mix $value)` | Check if the value is equal. <br/> param *mixed* `$value` - Value to match equal. |
84+
| `must_contain(str $chars)` | Check if the value must contains some charectors. <br/> param *string* `$chars` - Set of chars in one string. Ex. "@#&abc123"|
85+
| `match(str $pattern)` | Check if the value matchs a pattern. <br/> param *string* `$patarn` - Rejex pattern to match. |
86+
87+
## More
88+
- You can change default error response messages on `Validator.php` at line `20`.
89+
- For the pattern of `match()` method, check out [PHP Manual](https://www.php.net/manual/en/function.preg-match.php) and [W3Schools](https://www.w3schools.com/php/php_regex.asp).
90+
91+
## LICENSE
92+
[MIT License](LICENSE)

0 commit comments

Comments
 (0)