add validation
This commit is contained in:
1 parent
e9e7cd6437
commit
3c34fe1207
8 files changed
+313
-4
No files matched your search
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
namespace Hekmatinasser\Verta;
|
||||
|
||||
|
||||
class JalaliValidator
|
||||
{
|
||||
public function validateDate($attribute, $value, $parameters)
|
||||
{
|
||||
if (!is_string($value)) {
|
||||
return false;
|
||||
}
|
||||
$format = count($parameters) ? $parameters[0] : 'Y/m/d';
|
||||
try {
|
||||
Verta::parseFormat($format, $value);
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validateDateTime($attribute, $value, $parameters)
|
||||
{
|
||||
if (!is_string($value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$format = count($parameters) ? $parameters[0] : 'Y/m/d h:i:s';
|
||||
try {
|
||||
Verta::parseFormat($format, $value);
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validateDateAfter($attribute, $value, $parameters)
|
||||
{
|
||||
if (!is_string($value)) {
|
||||
return false;
|
||||
}
|
||||
$format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d';
|
||||
try {
|
||||
$base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : new Verta();
|
||||
return Verta::parseFormat($format, $value)->greaterThan($base);
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function validateDateTimeAfter($attribute, $value, $parameters)
|
||||
{
|
||||
if (!is_string($value)) {
|
||||
return false;
|
||||
}
|
||||
$format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d H:i:s';
|
||||
try {
|
||||
$base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : new Verta();
|
||||
return Verta::parseFormat($format, $value)->greaterThan($base);
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function validateDateBefore($attribute, $value, $parameters)
|
||||
{
|
||||
if (!is_string($value)) {
|
||||
return false;
|
||||
}
|
||||
$format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d';
|
||||
try {
|
||||
$base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : new Verta();
|
||||
return Verta::parseFormat($format, $value)->lessThan($base);
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function validateDateTimeBefore($attribute, $value, $parameters)
|
||||
{
|
||||
if (!is_string($value)) {
|
||||
return false;
|
||||
}
|
||||
$format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d H:i:s';
|
||||
try {
|
||||
$base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : new Verta();
|
||||
return Verta::parseFormat($format, $value)->lessThan($base);
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function replaceDate($message, $attribute, $rule, $parameters)
|
||||
{
|
||||
return $message;
|
||||
}
|
||||
|
||||
public function replaceDatetime($message, $attribute, $rule, $parameters)
|
||||
{
|
||||
return $message;
|
||||
}
|
||||
|
||||
public function replaceDateAfterOrBefore($message, $attribute, $rule, $parameters)
|
||||
{
|
||||
$format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d';
|
||||
$date = count($parameters) ? $parameters[0] : Verta::instance()->format($format);
|
||||
$faDate = Verta::persianNumbers($date);
|
||||
return str_replace([':date', ':fa-date'], [$date, $faDate], $message);
|
||||
}
|
||||
|
||||
public function replaceDateTimeAfterOrBefore($message, $attribute, $rule, $parameters)
|
||||
{
|
||||
$format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d H:i:s';
|
||||
$date = count($parameters) ? $parameters[0] : Verta::instance()->format($format);
|
||||
$faDate = Verta::persianNumbers($date);
|
||||
return str_replace([':date', ':fa-date'], [$date, $faDate], $message);
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -139,7 +139,7 @@ class Verta extends DateTime {
|
||||
* @var array
|
||||
*/
|
||||
protected static $monthYear = array(
|
||||
'فرودین',
|
||||
'فروردین',
|
||||
'اردیبهشت',
|
||||
'خرداد',
|
||||
'تیر',
|
||||
@@ -381,7 +381,7 @@ class Verta extends DateTime {
|
||||
$datetime = str_replace(self::$monthYear, range(1,12), $datetime);
|
||||
|
||||
$parse = date_parse_from_format($format, $datetime);
|
||||
if($parse['error_count'] == 0){
|
||||
if($parse['error_count'] == 0 && self::isValideDate($parse['year'], $parse['month'], $parse['day']) && self::isValideTime($parse['hour'], $parse['minute'], $parse['second'])){
|
||||
list($year, $month, $day) = self::getGregorian($parse['year'], $parse['month'], $parse['day']);
|
||||
list($hour,$minute, $second) = array($parse['hour'], $parse['minute'], $parse['second']);
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Hekmatinasser\Verta;
|
||||
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class VertaServiceProvider extends ServiceProvider
|
||||
@@ -13,7 +14,7 @@ class VertaServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
//
|
||||
$this->validator();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -38,4 +39,21 @@ class VertaServiceProvider extends ServiceProvider
|
||||
{
|
||||
return array('verta');
|
||||
}
|
||||
|
||||
protected function validator()
|
||||
{
|
||||
Validator::extend('jdate', JalaliValidator::class . '@validateDate');
|
||||
Validator::extend('jdatetime', JalaliValidator::class . '@validateDateTime');
|
||||
Validator::extend('jdate_after', JalaliValidator::class . '@validateDateAfter');
|
||||
Validator::extend('jdatetime_after', JalaliValidator::class . '@validateDateTimeAfter');
|
||||
Validator::extend('jdate_before', JalaliValidator::class . '@validateDateBefore');
|
||||
Validator::extend('jdatetime_before', JalaliValidator::class . '@validateDateTimeBefore');
|
||||
|
||||
Validator::replacer('jdate', JalaliValidator::class . '@replaceDate');
|
||||
Validator::replacer('jdatetime', JalaliValidator::class . '@replaceDateTime');
|
||||
Validator::replacer('jdate_after', JalaliValidator::class . '@replaceDateAfterOrBefore');
|
||||
Validator::replacer('jdatetime_after', JalaliValidator::class . '@replaceDateTimeAfterOrBefore');
|
||||
Validator::replacer('jdate_before', JalaliValidator::class . '@replaceDateAfterOrBefore');
|
||||
Validator::replacer('jdatetime_before', JalaliValidator::class . '@replaceDateTimeAfterOrBefore');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'jdate' => 'The :attribute is not valid Jalali date',
|
||||
'jdatetime' => 'The :attribute is not valid Jalali datetime',
|
||||
'jdate_after' => 'The :attribute must be a Jalali date after :date.',
|
||||
'jdatetime_after' => 'The :attribute must be a Jalali datetime after :date.',
|
||||
'jdate_before' => 'The :attribute must be a Jalali date before :date.',
|
||||
'jdatetime_before' => 'The :attribute must be a Jalali datetime before :date.',
|
||||
];
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'jdate' => ':attribute' . ' تاریخ شمسی معتبر نمی باشد.',
|
||||
'jdatetime' => ':attribute' . ' تاریخ و زمان شمسی معتبر نمی باشد.',
|
||||
'jdate_after' => ':attribute' . ' تاریخ شمسی باید بعد از ' . ':fa-date' . ' باشد.',
|
||||
'jdatetime_after' => ':attribute' . ' تاریخ و زمان شمسی باید بعد از ' . ':fa-date' . ' باشد.',
|
||||
'jdate_before' => ':attribute' . ' تاریخ شمسی باید قبل از ' . ':fa-date' . ' باشد.',
|
||||
'jdatetime_before' => ':attribute' . ' تاریخ و زمان شمسی باید قبل از ' . ':fa-date' . ' باشد.',
|
||||
];
|
||||
Reference in new issue
Block a user