Elegant validation dates in laravel need to be greater than today

Time:2021-3-10
  • Sometimes we come across a scene, such as buying a ticket. We need to query the remaining ticket. At this time, we need a date range, the start date must be greater than or equal to today, and the end date must be greater than or equal to the start date.

Version

  • Laravel 5.5.40
  • PHP 7.1

Before you start, for the convenience of viewing the results, in the/Exceptions/Handler.phpThe file caught an exception
Elegant validation dates in laravel need to be greater than today
Let’s start with a verification

  • New onerequestAnd inject it into the method that needs to verify the controller

Elegant validation dates in laravel need to be greater than today

  • The red part circled in the figure below should be changed toreturn trueBecause we don’t usually authenticate here

Elegant validation dates in laravel need to be greater than today

  • As shown in the picture above,end_dateIt’s easy to get throughstart_dateTo limit the time frame, thenstart_dateHow to verify? That’s what I thought.

Elegant validation dates in laravel need to be greater than today

  • By adding an extra fieldcurr_date, and thenstart_dateLimit the scope by this field. The test found that it didn’t work. It directly enters the data printed by the controller

Elegant validation dates in laravel need to be greater than today

  • I debugged it. I’m hereIlluminate\Validation\Concerns、ValidatesAttributes::validateAfterOrEqualMethod jump tocompareDatesMethod and try to print it$this->getValue($parameters[0])At this time$paramters[0]bycurr_datealwaysnull

Elegant validation dates in laravel need to be greater than today
Elegant validation dates in laravel need to be greater than today

  • Then looked up the source code, knew the reason, said directly can correctly verify the method, said the detailed process.
  • The right way is this

Elegant validation dates in laravel need to be greater than today

  • Overriding theprepareForValidationMethod, and then test

Elegant validation dates in laravel need to be greater than today

  • This is the time to get the verification you want

  • Analyze the process
  • Thanks for being able to instantiate the object we want through dependency injectionLaravelOfIoCcontainer
  • Every time the container parses a new object, it always releases some bound events

Elegant validation dates in laravel need to be greater than today

  • Many of these events are bound by service providers. Let’s look at them directlyconfig/app.phpService providers for form validation in

Elegant validation dates in laravel need to be greater than today
Elegant validation dates in laravel need to be greater than today
Elegant validation dates in laravel need to be greater than today

  • We can see that when theValidatesWhenResolvedObject is called automatically after parsingvalidatemethod
  • Back to the newIndexTicketRequestClass, without this method, go to the parent class and find the implementationValidatesWhenResolved, andvalidateIt’s intraitOfValidatesWhenResolvedTrait

Elegant validation dates in laravel need to be greater than today

  • This class finds the answer invalidateThere are three main steps in the method
  • $this->prepareForValidation()Preparation before validation
  • Create a new validation instance
  • Start validation
  • The reason is that it needs to be set before validationcurr_dateLet’s take a look at the new verification instance to see the answer

Elegant validation dates in laravel need to be greater than today
Elegant validation dates in laravel need to be greater than today

  • Here is a place that leads to our problem, that is to call first$this->validationData()How to get itrequestAnd then through the$this->container->call([$this, 'rules'])Get the validation rules, so we’re hererulesMethod writing naturally does not take effect, and subsequent verification cannot continue

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]