Last active
December 20, 2019 02:48
-
-
Save mabasic/8cf7f1a895218ca466d9 to your computer and use it in GitHub Desktop.
Custom Array Validation for Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ArrayValidation extends Illuminate\Validation\Validator | |
{ | |
public function validatePeriodArray($field, $values, $params) | |
{ | |
$valid = true; | |
foreach($values as $value) | |
{ | |
$validator = Validator::make( | |
array('period' => $value), | |
array('period' => array('required', 'date_format:d-m-Y - d-m-Y')) | |
); | |
if ($validator->fails()) | |
{ | |
$valid = false; | |
break; | |
} | |
} | |
if($valid) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
public function validatePricePerDayArray($field, $values, $params) | |
{ | |
$valid = true; | |
foreach($values as $value) | |
{ | |
$validator = Validator::make( | |
array('price_per_day' => $value), | |
array('price_per_day' => array('required', 'numeric', 'min:0')) | |
); | |
if ($validator->fails()) | |
{ | |
$valid = false; | |
break; | |
} | |
} | |
if($valid) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//... other stuff | |
/* | |
ArrayValidation class that extends Validation class | |
*/ | |
Validator::resolver(function($translator, $data, $rules, $messages) | |
{ | |
return new ArrayValidation($translator, $data, $rules, $messages); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you, can you check my second question in laracasts forum? tia.