2
votes

I have a form, it has a date field called starts, which is optional:

...
<input type="date" name="starts">
...

Then I created a Request in Laravel that checks, among other things, if starts is a date, so the rules() function in the Request is somthing like:

public function rules()
{
  return [
    ... (more validation rules)
    'starts'             => 'date',
    ... (more validation rules)
  ];
}

So, when I submit the form and there's no starts, the validation rules kicks in, it says it's not a date, and returns to my form.

How do I tell Laravel that starts is a date, only if it is present?

I'm looking something like the validation in files, which only take place if there's a file present

2
try required_if. for more detail laravel.com/docs/5.0/validation#rule-required-ifGaurav Gupta
You can use sometimes rule.Tpojka

2 Answers

5
votes

You can add "nullable" for your laravel validation rule like so:

'starts' => 'date|nullable'
0
votes

Try 'starts' => 'nullable|date' as validation rule. There is a section about this in the documentation.