2
votes

I want to validate a date field in my form and I also have a date field in a table and I want to check if the date in the form is after the date in the table. Can I do this with the Validation function after:date? I think with this function I need to define the date in the model (where I got the validation rules). Or do I need to check it with a custom validation rule? Thanks

1

1 Answers

1
votes

You should be able to use the after:date validation if you retrieve the date to be checked against from the database table first, eg.

$date_from_table = call to retrieve date from table;

$validator = Validator::make(
    array('date' => $date_from_form),
    array('date' => 'after:'.$date_from_table)
);

Which as you note could go in the model if that is where you are defining the validation rules - in which case it would probably be slightly different to the above as you may not be making the validator instance in the model, just specifying the validation rules, eg.

protected $rules = array(
    'date' => 'after:'.$this->date_from_table
);

If this works it probably isn't worth the bother of creating a custom validation rule unless you are going to be doing the same sort of thing in a number of different models.