Saturday, April 15, 2017

Laravel Validation sometimes rules for date validation

I currently have a validation rule which looks like this:

  public function rules()
  {
   return [
        'startDate' => 'required|sometimes|before_or_equal:endDate',
        'endDate' => 'sometimes|required|after_or_equal:startDate',
    ];
  }

The sometimes option works as I understand it on the basis that if the field is present, run the validation rule. However, if the end date is not sent or is null, my before or equal rule kicks in and fails. In some instances within my application, end date will be null. Is there a way to 'cancel' the startDate validation rule in this instance or would I need to create a custom validator for this purpose?

something like before_or_equal_when_present ?



via Paul

Advertisement