1

I need to extend laravel validator creating a new validator but the problem is that I need to pass 2 parameters, one for days and one for slots. How to solve this?

Example dd($request->all) dump:

array:2 [
  "days" => array:2 [
    0 => "1" // Mon
    1 => "2" // Tue
  ]
  "slots" => array:2 [
    1 => array:2 [
      "open" => "09:00"
      "close" => "11:30"
    ]
    2 => array:2 [
      "open" => "16:00"
      "close" => "21:00"
    ]
  ]
]

It need to loop through days and check with slots.

Pseudo code, example:

foreach($days as $day) {
  foreach($slots as $slot) 
   {
      // Validation Logic for $day and $slot (open and close)
   }
}
I'll-Be-Back
  • 10,530
  • 37
  • 110
  • 213

2 Answers2

2

This is the correct method of making a custom Laravel Validation

Create your own Validation Service Provider using

php artisan make:provider ValidationServiceProvider

then go to config\app.php and add this to providers

App\Providers\ValidationServiceProvider::class

Now go to ValidationServiceProvider.php and add

use Validator;

to the top...

and this in the boot() function

Validator::extend('days_with_slots', function($attribute, $value, $parameters, $validator) {
  $slots = request()->get('slots');

  if(!is_array($slots)) return false;

  foreach($days as $day) {
    foreach($slots as $slot) {
      if(empty($slot[$day]) || empty($slot[$day]['open'] || empty($slot[$day]['close']))) {
        return false;
      }
    }
  }
});

Finally, use this in your rules

$rules['slots'] = 'days_with_slots'

You can also add a custom message for it, say

$message['days_with_slots'] = 'Open and Close Timings are required for the days selected'

Hope this helps :)

prateekkathal
  • 3,482
  • 1
  • 20
  • 40
  • How can I define custom error message in `Validator::extend`? Each error message should have slots key – I'll-Be-Back Nov 17 '16 at 12:48
  • I want to put the value of open and close in the error message. – I'll-Be-Back Nov 17 '16 at 12:53
  • @I'll-Be-Back Sry for the late reply... Please tag me next time so I know... I wasn't notified for this for some reason. You can add all extended validations messages to either a global list... `resources/lang/validations.php` or make ur own Validator (by extending Illuminate\Validation\Validator) and overwrite the messages function with your own... and use your own Validator... – prateekkathal Dec 08 '16 at 06:37
1

Well, this is another approach.

  1. In your AppServiceProvider add your custom handler in boot() method:

    \Validator::resolver(function($translator, $data, $rules, $messages) {
        return new MyCustomValidator($translator, $data, $rules, $messages);
    });
    
  2. Create the resolver class:

    class MyCustomValidator extends Illuminate\Validation\Validator;
    {
        public function validateSlotsInDays($attribute, $value, $parameters){
            $valid = false;
            $days = \Request::input($parametros[0]);
            $slots = $value;
    
            foreach($days as $day) {
                foreach($slots as $slot) {
                    // Validation Logic for $day and $slot (open and close)
                }
             }
    
            return $valid;
        }
    
        public function validateSlot($attribute, $value, $parameters){
            $valid = false;
            $days = \Request::input($parametros[0]);
            $slot = $value;
            foreach($days as $day) {
                // do whatever here
            }
    
    
            return $valid;
        }
    
    }
    
  3. Finally, apply the rules. IF you need yo display an error if at least one slot is close, then you can use this rule:

    $rules = [
        'slots' => ['slots_in_days:days'],
    ];
    

    See how I am passing days as a parameter. In addition, if you need to validate each slot individually and display a message for each one of them, then apply the rule like this:

    $rules = [
        'slots.*' => ['slot:days'],
    ];
    

Addional docs:

Community
  • 1
  • 1
manix
  • 14,537
  • 11
  • 70
  • 107