Skip to content

Commit 3fdc027

Browse files
committed
Completely tested and documented date validation
1 parent 4bd9f9b commit 3fdc027

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ $data = [
2424
"password" => "pass@123",
2525
"confirm_password" => "pass@123",
2626
"sex" => "male",
27-
"phone" => "1236547895"
27+
"phone" => "1236547895",
28+
"dob" => "1998-07-11"
2829
];
2930
```
3031

@@ -44,6 +45,7 @@ $v->field('password')->required()->min_len(8)->max_len(16)->must_contain('@#$&')
4445
$v->field('confirm_password')->required()->equals($data['password']);
4546
$v->field('sex')->enum(['male', 'female', 'others']);
4647
$v->field('phone')->numeric()->min_len(10)->max_len(10);
48+
$v->field('dob')->date()->date_after('1998-01-01')->date_before('2002-12-31');
4749
```
4850
Make sure to run the field method on start of every method chain.
4951

@@ -81,12 +83,16 @@ Here is a list of the validators currently available.
8183
| `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. |
8284
| `enum(arr $list)` | Check if the value is in the list. <br/> param *array* `$list` - List of valid values. |
8385
| `equals(mix $value)` | Check if the value is equal. <br/> param *mixed* `$value` - Value to match equal. |
86+
| `date(string $date)` | Check if the value is a valid date. <br/> param *string* `$format` - Format of the date. (ex. Y-m-d) Check out [PHP Manual](https://www.php.net/manual/en/datetime.format.php) for more. |
87+
| `date_after(string $date)` | Check if the date appeared after the specified date. <br/> param *string* `$date` - Use format Y-m-d (ex. 2023-01-15). |
88+
| `date_before(string $date)` | Check if the date appeared before the specified date. <br/> param *string* `$date` - Use format Y-m-d (ex. 2023-01-15). |
8489
| `must_contain(str $chars)` | Check if the value must contains some charectors. <br/> param *string* `$chars` - Set of chars in one string. Ex. "@#&abc123"|
8590
| `match(str $pattern)` | Check if the value matchs a pattern. <br/> param *string* `$patarn` - Rejex pattern to match. |
8691

8792
## More
8893
- You can change default error response messages on `Validator.php` at line `20`.
8994
- 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).
95+
- To know more about date formats, check out [PHP Manual](https://www.php.net/manual/en/datetime.format.php).
9096

9197
## LICENSE
9298
[MIT License](LICENSE)

Validator.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ class Validator{
3030
"enum" => "{field} is invalid.",
3131
"equals" => "{field} does not match.",
3232
"must_contain" => "{field} must contains {chars}.",
33-
"match" => "{fiels} is invalid."
33+
"match" => "{field} is invalid.",
34+
"date" => "Date is invalid.",
35+
"date_after" => "{field} date is not valid.",
36+
"date_before" => "{field} date is not valid.",
3437
];
3538

3639
/**
@@ -254,6 +257,51 @@ function equals($value){
254257
return $this;
255258
}
256259

260+
/**
261+
* date - Check if the value is a valid date.
262+
*
263+
* @param mixed $format - format of the date. (ex. Y-m-d) Check out https://www.php.net/manual/en/datetime.format.php for more.
264+
* @return this
265+
*/
266+
function date($format = 'Y-m-d'){
267+
if($this->next && $this->exists()){
268+
$dateTime = DateTime::createFromFormat($format, $this->data[$this->current_field]);
269+
if(!($dateTime && $dateTime->format($format) == $this->data[$this->current_field])){
270+
$this->add_error_message('date');
271+
$this->next = false;
272+
}
273+
}
274+
return $this;
275+
}
276+
277+
/**
278+
* date_after - Check if the date appeared after the specified date.
279+
*
280+
* @param mixed $date - Use format Y-m-d (ex. 2023-01-15).
281+
* @return this
282+
*/
283+
function date_after($date){
284+
if($this->next && $this->exists() && strtotime($date) >= strtotime($this->data[$this->current_field])){
285+
$this->add_error_message('date_after');
286+
$this->next = false;
287+
}
288+
return $this;
289+
}
290+
291+
/**
292+
* date_before - Check if the date appeared before the specified date.
293+
*
294+
* @param mixed $date - Use format Y-m-d (ex. 2023-01-15).
295+
* @return this
296+
*/
297+
function date_before($date){
298+
if($this->next && $this->exists() && strtotime($date) <= strtotime($this->data[$this->current_field])){
299+
$this->add_error_message('date_before');
300+
$this->next = false;
301+
}
302+
return $this;
303+
}
304+
257305
/**
258306
* must_contain - Check if the value must contains some charectors.
259307
*

index.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
date_default_timezone_set("Asia/Kolkata");
3+
24
// Include the class file
35
require "Validator.php";
46

@@ -10,7 +12,8 @@
1012
"password" => "pass@123",
1113
"confirm_password" => "pass@123",
1214
"sex" => "male",
13-
"phone" => "1236547895"
15+
"phone" => "1236547895",
16+
"dob" => "1998-07-11"
1417
];
1518

1619
// Create a instance
@@ -24,6 +27,7 @@
2427
$v->field('confirm_password')->required()->equals($data['password']);
2528
$v->field('sex')->enum(['male', 'female', 'others']);
2629
$v->field('phone')->numeric()->min_len(10)->max_len(10);
30+
$v->field('dob')->date()->date_after('1998-01-01')->date_before('2002-12-31');
2731

2832
// Check if data is valid
2933
if(!$v->is_valid()){

0 commit comments

Comments
 (0)