Skip to content

Commit 50199b6

Browse files
committed
add Shamsi Date and Shamsi Date Between validation rules
1 parent f666f85 commit 50199b6

File tree

6 files changed

+158
-18
lines changed

6 files changed

+158
-18
lines changed

lang/en/persian-validation.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
'persian_alpha' => 'The :attribute must be a persian alphabet.',
55
'persian_num' => 'The :attribute must be a persian number.',
66
'persian_alpha_num' => 'The :attribute must be a persian alphabet or number.',
7-
'persian_alpha_eng_num' => 'The :attribute must be a persian alphabet or number or english number.',
7+
'persian_alpha_eng_num' => 'The :attribute must be a persian alphabet or number or english number.',
88
'persian_not_accept' => 'The :attribute could not be contain persian alphabet or number.',
9+
'shamsi_date' => 'The :attribute must be a shamsi (jalali) date.',
10+
'shamsi_date_between' => 'The :attribute must be between :afterDate and :beforeDate years of shamsi (jalali) date.',
911
'ir_mobile' => 'The :attribute must be a iranian mobile number.',
1012
'ir_phone' => 'The :attribute must be a iranian phone number.',
1113
'ir_phone_code' => 'The :attribute must be a iranian phone area code number.',

lang/fa/persian-validation.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
<?php
22

33
$invalidMsg = ':attribute معتبر نمی باشد.';
4+
$incorrectMsg = ':attribute صحیح نمی باشد.';
45

56
return [
67
'persian_alpha' => ':attribute فقط میتواند شامل حروف فارسی باشد.',
78
'persian_num' => ':attribute فقط میتواند شامل اعداد فارسی باشد.',
89
'persian_alpha_num' => ':attribute فقط میتواند شامل حروف و اعداد فارسی باشد.',
9-
'persian_alpha_eng_num' => ':attribute فقط میتواند شامل حروف و اعداد فارسی و اعداد انگلیسی باشد.',
10+
'persian_alpha_eng_num' => ':attribute فقط میتواند شامل حروف و اعداد فارسی و اعداد انگلیسی باشد.',
1011
'persian_not_accept' => ':attribute فقط میتواند شامل حروف یا اعداد لاتین باشد.',
11-
'ir_mobile' => $invalidMsg,
12-
'ir_phone' => $invalidMsg,
13-
'ir_phone_code' => $invalidMsg,
12+
'shamsi_date' => $incorrectMsg,
13+
'shamsi_date_between' => ':attribute باید بین سال های :afterDate تا :beforeDate باشد.',
14+
'ir_mobile' => $incorrectMsg,
15+
'ir_phone' => $incorrectMsg,
16+
'ir_phone_code' => $incorrectMsg,
1417
'ir_phone_with_code' => ':attribute باید بهمراه کد استان وارد شود.',
1518
'ir_postal_code' => $invalidMsg,
1619
'ir_card_number' => $invalidMsg,
1720
'ir_sheba' => $invalidMsg,
1821
'ir_national_code' => $invalidMsg,
19-
'a_url' => $invalidMsg,
20-
'a_domain' => $invalidMsg,
22+
'a_url' => $incorrectMsg,
23+
'a_domain' => $incorrectMsg,
2124
];

readme.md

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ You can access to validation rules by passing the rules key according blew follo
3232
| persian_alpha_num | Persian alphabet and numbers |صادق۱۲۳۴
3333
| persian_alpha_eng_num | Persian alphabet and numbers with english numbers |صادق۱۲34
3434
| persian_not_accept | Doesn't accept Persian alphabet and numbers | cant be persian
35+
| shamsi_date | Check shamsi (jalali) date with format(Y/m/d) | 1373/3/19
36+
| shamsi_date_between:1300,1400 | Check shamsi (jalali) date with format(Y/m/d) between years | 1373/3/19
3537
| ir_mobile | Iranian mobile numbers | 00989173456789, +989173456789, 989173456789, 09173456789, 91712345678
3638
| ir_phone | Iranian phone numbers | 37236445
3739
| ir_phone_code | Iranian phone area code | 077, 021, ...
@@ -47,7 +49,7 @@ You can access to validation rules by passing the rules key according blew follo
4749
### Persian Alpha
4850
Accept Persian language alphabet according to standard Persian, this is the way you can use this validation rule:
4951

50-
```
52+
``` php
5153
$input = [ 'فارسی' ];
5254

5355
$rules = [ 'persian_alpha' ];
@@ -58,7 +60,7 @@ Validator::make( $input, $rules );
5860
### Persian numbers
5961
Validate Persian standard numbers (۰۱۲۳۴۵۶۷۸۹):
6062

61-
```
63+
``` php
6264
$input = [ '۰۱۲۳۴۵۶۷۸۹' ];
6365

6466
$rules = [ 'persian_num' ];
@@ -69,7 +71,7 @@ Validator::make( $input, $rules );
6971
### Persian Alpha Num
7072
Validate Persian alpha num:
7173

72-
```
74+
``` php
7375
$input = [ '۰فارسی۱۲۳۴۵۶۷۸۹' ];
7476

7577
$rules = [ 'persian_alpha_num' ];
@@ -80,18 +82,40 @@ Validator::make( $input, $rules );
8082
### Persian Alpha Eng Num
8183
Validate Persian alpha num with english num:
8284

83-
```
85+
``` php
8486
$input = [ '۰فارسی۱۲۳۴۵6789' ];
8587

8688
$rules = [ 'persian_alpha_eng_num' ];
8789

8890
Validator::make( $input, $rules );
8991
```
9092

93+
### Shamsi Date
94+
Validate shamsi (jalali) date:
95+
96+
``` php
97+
$input = [ '1373/3/19' ];
98+
99+
$rules = [ 'shamsi_date' ];
100+
101+
Validator::make( $input, $rules );
102+
```
103+
104+
### Shamsi Date Between
105+
Validate shamsi (jalali) date between years:
106+
107+
``` php
108+
$input = [ '1373/3/19' ];
109+
110+
$rules = [ 'shamsi_date_between:1300,1400' ];
111+
112+
Validator::make( $input, $rules );
113+
```
114+
91115
### Iran mobile phone
92116
Validate Iranian mobile numbers (Irancell, Rightel, Hamrah-e-aval, ...):
93117

94-
```
118+
``` php
95119
$input = [ '09381234567' ];
96120

97121
$rules = [ 'ir_mobile' ];
@@ -102,7 +126,7 @@ Validator::make( $input, $rules );
102126
### Sheba number
103127
Validate Iranian bank sheba numbers:
104128

105-
```
129+
``` php
106130
$input = [ 'IR062960000000100324200001' ];
107131

108132
$rules = [ 'ir_sheba' ];
@@ -113,7 +137,7 @@ Validator::make( $input, $rules );
113137
### Iran national code
114138
Validate Iranian national code (Melli Code):
115139

116-
```
140+
``` php
117141
$input = [ '3240175800' ];
118142

119143
$rules = [ 'ir_national_code' ];
@@ -124,7 +148,7 @@ Validator::make( $input, $rules );
124148
### Payment card number
125149
Validate Iranian bank payment card numbers:
126150

127-
```
151+
``` php
128152
$input = [ '6274129005473742' ];
129153

130154
$rules = [ 'ir_card_number' ];
@@ -135,7 +159,7 @@ Validator::make( $input, $rules );
135159
### Iran postal code
136160
Validate Iranian postal code:
137161

138-
```
162+
``` php
139163
$input = [ '167197-35744' ];
140164

141165
$rules = [ 'ir_postal_code' ];
@@ -166,6 +190,10 @@ Validator::make( $request->all(), [
166190

167191
'address' => 'persian_alpha_eng_num', // Validate persian alphabet & numbers with english numbers
168192

193+
'birthday' => 'shamsi_date', // Validate shamsi date
194+
195+
'start_date' => 'shamsi_date_between:1300,1400', // Validate shamsi date between years
196+
169197
'mobile' => 'ir_mobile', // Validate mobile number
170198

171199
'sheba_number' => 'ir_sheba', // Validate sheba number of bank account

src/PersianValidationServiceProvider.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ class PersianValidationServiceProvider extends ServiceProvider
1818
'persian_alpha' => 'PersianAlpha',
1919
'persian_num' => 'PersianNumber',
2020
'persian_alpha_num' => 'PersianAlphaNumber',
21-
'persian_alpha_eng_num' => 'PersianAlphaEngNumber',
21+
'persian_alpha_eng_num' => 'PersianAlphaEngNumber',
2222
'persian_not_accept' => 'PersianNotAccept',
23+
'shamsi_date' => 'ShamsiDate',
24+
'shamsi_date_between' => 'ShamsiDateBetween',
2325
'ir_mobile' => 'IranianMobile',
2426
'ir_phone' => 'IranianPhone',
2527
'ir_phone_code' => 'IranianPhoneAreaCode',
@@ -54,8 +56,12 @@ public function boot()
5456

5557
foreach($this->validatorsMap as $name => $method)
5658
{
57-
Validator::extend($name, PersianValidators::class."@validate{$method}",
59+
Validator::extend($name, PersianValidators::class . "@validate{$method}",
5860
__("{$langNamespace}::{$langFileName}.{$name}"));
61+
62+
if (method_exists(PersianValidators::class, "replace{$method}")) {
63+
Validator::replacer($name, PersianValidators::class . "@replace{$method}");
64+
}
5965
}
6066
}
6167

src/PersianValidators.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,74 @@ public function validatePersianNotAccept($attribute, $value, $parameters)
7979
return false;
8080
}
8181

82+
/**
83+
* Validate shamsi (jalali) date
84+
*
85+
* @param $attribute
86+
* @param $value
87+
* @param $parameters
88+
* @return bool
89+
*/
90+
public function validateShamsiDate($attribute, $value, $parameters)
91+
{
92+
$jdate = explode('/', $value);
93+
return (count($jdate) === 3 && $this->isValidjDate($jdate[0], $jdate[1], $jdate[2]));
94+
}
95+
96+
/**
97+
* Validate shamsi (jalali) date between years
98+
*
99+
* @param $attribute
100+
* @param $value
101+
* @param $parameters
102+
* @return bool
103+
*/
104+
public function validateShamsiDateBetween($attribute, $value, $parameters)
105+
{
106+
$jdate = explode('/', $value);
107+
return $this->validateShamsiDate($attribute, $value, $parameters) && ($parameters[0] <= $jdate[0] && $parameters[1] >= $jdate[0]);
108+
}
109+
110+
/**
111+
* Replace validate message for ShamsiDateBetween
112+
*
113+
* @param $message
114+
* @param $attribute
115+
* @param $rule
116+
* @param $parameters
117+
* @return bool
118+
*/
119+
public function replaceShamsiDateBetween($message, $attribute, $rule, $parameters)
120+
{
121+
return str_replace([':afterDate', ':beforeDate'], [$parameters[0], $parameters[1]], $message);
122+
}
123+
124+
/**
125+
* Validate a jalali date (jalali equivalent of php checkdate() function)
126+
* Refer to: https://github.com/hekmatinasser/verta (v1.11.5) => Comparison Trait => isValidDate function
127+
*
128+
* @param int $month
129+
* @param int $day
130+
* @param int $year
131+
* @return bool
132+
*/
133+
private function isValidjDate($year, $month, $day) {
134+
if($year < 0 || $year > 32766) {
135+
return false;
136+
}
137+
if($month < 1 || $month > 12) {
138+
return false;
139+
}
140+
141+
$daysMonthJalali = [31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29];
142+
$dayLastMonthJalali = in_array(($year % 33) , [1 , 5 , 9 , 13 ,17 , 22 , 26 , 30]) && ($month == 12) ? 30 : $daysMonthJalali[intval($month)-1];
143+
if($day < 1 || $day > $dayLastMonthJalali) {
144+
return false;
145+
}
146+
147+
return true;
148+
}
149+
82150
/**
83151
* Validate iranian mobile number.
84152
*

tests/PersianValidationTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,39 @@ public function testPersianAlphaEngNumber()
150150
$this->assertEquals(true, $this->persianValidator->validatePersianAlphaEngNumber($this->attribute, $this->value, $this->parameters));
151151
}
152152

153+
/**
154+
* Unit test of shamsi date
155+
*
156+
* @return void
157+
*/
158+
public function testShamsiDate()
159+
{
160+
$this->value = "1373/3/19";
161+
$this->assertEquals(true, $this->persianValidator->validateShamsiDate($this->attribute, $this->value, $this->parameters));
162+
163+
$this->value = "1234";
164+
$this->assertEquals(false, $this->persianValidator->validateShamsiDate($this->attribute, $this->value, $this->parameters));
165+
}
166+
167+
/**
168+
* Unit test of shamsi date between years
169+
*
170+
* @return void
171+
*/
172+
public function testShamsiDateBetween()
173+
{
174+
$this->value = "1373/3/19";
175+
$this->parameters = [1300, 1400];
176+
$this->assertEquals(true, $this->persianValidator->validateShamsiDateBetween($this->attribute, $this->value, $this->parameters));
177+
178+
$this->value = "1401/1/1";
179+
$this->parameters = [1300, 1400];
180+
$this->assertEquals(false, $this->persianValidator->validateShamsiDateBetween($this->attribute, $this->value, $this->parameters));
181+
182+
$this->value = "1234";
183+
$this->assertEquals(false, $this->persianValidator->validateShamsiDate($this->attribute, $this->value, $this->parameters));
184+
}
185+
153186
/**
154187
* Unit test of iranian mobile number
155188
*

0 commit comments

Comments
 (0)