Skip to content

Instantly share code, notes, and snippets.

@rmariuzzo
Created October 8, 2013 04:38
Show Gist options
  • Save rmariuzzo/6879532 to your computer and use it in GitHub Desktop.
Save rmariuzzo/6879532 to your computer and use it in GitHub Desktop.
A simple Laravel 4 validator that ensure the field under validation must match the format defined according to the `Carbon\Carbon::createFromFormat` method.
<?php
use Carbon\Carbon;
use Illuminate\Validation\Validator;
class CustomValidator extends Validator {
# carbon_date_format #
public function validateCarbonDateFormat($attribute, $value, $parameters)
{
try {
Carbon::createFromFormat($parameters[0], $value);
return true;
} catch (Exception $e) {
return false;
}
}
protected function replaceCarbonDateFormat($message, $attribute, $rule, $parameters)
{
return str_replace(':format', $parameters[0], $message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment