V2: need to validate that date ranges are between outer date limits

Hi, I’m using October version 2.x and have a form that has a “master” date range, and then a series of “sub” date ranges that must be within the start and end dates of the master date range.

For for example, if the master date range start = 2024-01-01 and end = 2024-05-01, then all of the sub date ranges in the form must have start and end dates between those two master date range dates.
valid:
2024-01-01 to 2024-03-01
2024-02-01 to 2024-05-01

invalid:
2023-12-31 to 2024-03-01 (start date is outside of range)
2024-01-01 to 2024-05-02 (end date is outside of range)

I’ve read up on the custom validation but the docs seem incomplete. It’s very vague about creating the rules file, and refers to the laravel docs which say to use make:rule, but that’s not even a command in october, etc.

Any insight or help appreciated! Thanks!

I can see 3 ways.

  1. You setup in your model the rule. Not tested
use Model;

class YourModel extends Model
{
    public $rules = [
        'sub_date' => 'after:2024-01-01|before:2024-05-01',
    ];

    // Other model code...
}
  1. In your beforeSave function…
use Model;

class YourModel extends Model
{
    // Other model code...

    public function beforeSave()
    {
        $startDate = strtotime('2024-01-01');
        $endDate = strtotime('2024-05-01');

        if (strtotime($this->start_date) <= $startDate) {
            throw new \Exception('Start date must be after January 1, 2024.');
        }

        if (strtotime($this->end_date) >= $endDate) {
            throw new \Exception('End date must be before May 1, 2024.');
        }
    }
}

  1. Define a global rule : Validation - October CMS - 3.x
1 Like

I use the beforeValidate event for that purpose and extend the rules in there.
Example with some comments:

    public function beforeValidate()
    {
      
       /* base rules
        *   [
        *       'other_date_start' => 'required|date',
        *       'other_date_end' => 'required|date|after:other_date_start',
        *   ]
        */
        
        // throws an exception if any of the dates are invalid
        $masterDateStart = Argon::parse(post('master_date_start'));
        $masterDateEnd = Argon::parse(post('master_date_end'));
        
        // maybe check if the Argon instances are valid
        $this->rules['other_date_start'] .= '|after_or_equal:' . $masterDateStart->format('Y-m-d') . '|before_or_equal:' . $masterDateEnd->format('Y-m-d');
        $this->rules['other_date_end'] .= '|after_or_equal:' . $masterDateStart->format('Y-m-d') . '|before_or_equal:' . $masterDateEnd->format('Y-m-d');
    }

Hope that helps!

The ‘after_or_equal’ rule is helpful for another issue I was having, didn’t know that existed since it’s not in the documentation (Validation - October CMS - 2.x). Is there anywhere where I can see a full list of all validation rules?

This will work, thank you!

October is based on Laravel, you can always take a look in Laravel docs!

1 Like

I see, and for october cms v2, it would be laravel 6, so: Validation - Laravel 6.x - The PHP Framework For Web Artisans (validation#available-validation-rules)

Thanks!

1 Like