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); | |
}); |
Thank you, can you check my second question in laracasts forum? tia.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use this as a template for custom validation rules or see the documentation: http://laravel.com/docs/validation#custom-validation-rules