namespace Lame\Extensions\Validators; use Illuminate\Validation\Validator; class TimestampValidator extends Validator{ }
/** , 10 , timestamp */ $date = Carbon::now()->subYears(10)->addDay(1)->timestamp; /** , timestamp timestamp */ $rules = [ "bDay" => "numeric|before_timestamp:".$date, ];
public function validateBeforeTimestamp($attribute, $value, $parameters) { $value = (int)$value; if ((int)$parameters[0] <= 0) { throw new \Exception("Timestamp parameter in the beforeTimestamp validator not valid!"); } if ($value != "" && $value >= $parameters[0]) { return false; } return true; }
$date = Carbon::now()->addHours(4)->timestamp; $rules = [ "deadline" => "required|numeric|after_timestamp:".$date ];
public function validateAfterTimestamp($attribute, $value, $parameters) { $value = (int)$value; if ((int)$parameters[0] <= 0) { throw new \Exception("Timestamp parameter in the beforeTimestamp validator not valid!"); } if ($value != "" && $value <= $parameters[0]) { return false; } return true; }
<?php namespace Lame\Providers; use Illuminate\Support\ServiceProvider; use Lame\Extensions\Validators\TimestampValidator; use Validator; class CustomValidateServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { Validator::resolver(function ($translator, $data, $rules, $messages) { return new TimestampValidator($translator, $data, $rules, $messages); }); } /** * Register any application services. * * @return void */ public function register() { // } }
"custom" => [ "deadline" => [ "after_timestamp" => "Deadline should be minimum 4 hours" ], "bDay" => [ "before_timestamp" => "Age should be minimum 10 years", "numeric" => "Birthday date should be in timestamp" ] ]
<?php namespace Lame\Extensions\Validators; use Illuminate\Validation\Validator; class TimestampValidator extends Validator { #region timestamp valitators - after_timestamp:{timestamp} | before_timestamp:{timestamp} /** * @param $attribute * @param $value * @param $parameters = ["date" => "Date before which should be input timestamp"] * @return bool * @throws \Exception */ public function validateBeforeTimestamp($attribute, $value, $parameters) { $value = (int)$value; if ((int)$parameters[0] <= 0) { throw new \Exception("Timestamp parameter in the beforeTimestamp validator not valid!"); } if ($value != "" && $value >= $parameters[0]) { return false; } return true; } /** * @param $attribute * @param $value * @param $parameters = ["date" => "Date before which should be input timestamp"] * @return bool * @throws \Exception */ public function validateAfterTimestamp($attribute, $value, $parameters) { $value = (int)$value; if ((int)$parameters[0] <= 0) { throw new \Exception("Timestamp parameter in the beforeTimestamp validator not valid!"); } if ($value != "" && $value <= $parameters[0]) { return false; } return true; } #endregion }
Source: https://habr.com/ru/post/281827/
All Articles