0

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)

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.