📜 ⬆️ ⬇️

Laravel Timestamp Validator

image

Laravel 5.1, Laravel 5.2, Lara ... The code progresses, optimizes and develops. In the new (5.2) version, the array validator appeared, for example, but what if you need to validate the incoming timestamp? That's right, write a crutch your decision.

Lived was and lives one project on Laravel 5.1. More precisely, its API lives side. There is a need to "drive" back and forth various dates. But how to drive them if there are time zones? It was decided to set the server to UTC + 0 and communicate using the timestamp, which at the frontend is easily converted at the right time. Ok, no questions on this. Except for one - how to validate incoming data? Create your own validator.

Full validator code at the very end of the article.
')
Go!

In the app / Extensions / Validators folder, create a file and name it TimestampValidator.php .

namespace Lame\Extensions\Validators; use Illuminate\Validation\Validator; class TimestampValidator extends Validator{ } 

We need to accept that the incoming date matches the “before” and “after”.

Consider the first example. We have a date of birth of the user. The user must be older than 10 years, i.e. born to 2016. Accordingly, we need to accept the date, which will be until 2016. In the validation rules we specify:

 /**   ,  10 ,      timestamp    */ $date = Carbon::now()->subYears(10)->addDay(1)->timestamp; /** ,      timestamp       timestamp */ $rules = [ "bDay" => "numeric|before_timestamp:".$date, ]; 

The “before_timestamp” rule has appeared. We return to our validator and create a method that will perform the necessary checks. The method name should have the following structure: "validate <rule in camelCase format>". $ value among the input parameters is the value that came from outside. $ parameters - an array of parameters that are specified in the rules (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; } 

The second example. We need to create a task with a deadline. The minimum deadline is 4 hours. Create rules:

 $date = Carbon::now()->addHours(4)->timestamp; $rules = [ "deadline" => "required|numeric|after_timestamp:".$date ]; 

A new rule has appeared - “after_timestamp”. We process it in our validator:

 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; } 

To connect our validator, I created my ServiceProvider in the app / Providers folder - CustomValidateServiceProvider.php .

 <?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() { // } } 

On this, in principle, everything. Error messages are indicated in the validation.php file.

 "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" ] ] 

Full class code
 <?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 } 


With the help date , after , before timestamp you will not check. Or you can check? If possible, I will be glad to read the existing options in the comments, messages.

Source: https://habr.com/ru/post/281827/


All Articles