Reputation: 2139
I am trying to create a few validation rules for an input form using Laravel 4.x and I have also go the Validator service from Laracasts.
Is there any way to use the current year, taken from the system date, and use that in the rules?
For example, I want to create a rule that says that the year entered in the input form does not exceed the current year (i.e : I cannot put in 2015 or any higher year as of this day)
Upvotes: 0
Views: 1279
Reputation: 388
You can create new validation rule like this:
// Less than equal
Validator::extend('lte', function($attribute, $value, $parameters)
{
if ( (float) $value <= (float) $parameters[0] )
return true;
});
Use the new rule
$input = array('year' => 2015);
$rules = array('year' => 'lte:'.date('Y'));
$validation = Validator::make($input, $rules);
Upvotes: 0
Reputation: 2208
You could do this:
'date' => array('min:'.date('Y'), 'max:'.date('Y'), 'date_format:"Y"');
Something along that line.. Catch my drift though right?
Note: This is untested.
PS: If for arguments sake you have a different format than just the year, you could split the inputs just for the validation.
$input = Input::only('date');
$input['date-year'] = date('Y', strtotime($input['date']));
//And then the validation rule will look something like this...
$rules['date-year'] = array('min:'.date('Y'), 'max:'.date('Y'), 'date_format:"Y"');
Hope that helped.
Upvotes: 2