Skip to content

Commit 226d33a

Browse files
authored
Add cron expression form validation (alexjustesen#279)
1 parent 6bb6c9d commit 226d33a

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

app/Filament/Pages/Settings/GeneralPage.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Filament\Pages\Settings;
44

5+
use App\Rules\ValidCronExpression;
56
use App\Settings\GeneralSettings;
67
use Filament\Forms\Components\Card;
78
use Filament\Forms\Components\Grid;
@@ -64,6 +65,7 @@ protected function getFormSchema(): array
6465
Section::make('Speedtest Settings')
6566
->schema([
6667
TextInput::make('speedtest_schedule')
68+
->rules([new ValidCronExpression()])
6769
->helperText('Leave empty to disable the schedule. You can also use the cron expression generator [HERE](https://crontab.cronhub.io/) to help you make schedules.')
6870
->nullable()
6971
->columnSpan(1),

app/Rules/ValidCronExpression.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Rules;
4+
5+
use Cron\CronExpression;
6+
use Illuminate\Contracts\Validation\InvokableRule;
7+
8+
class ValidCronExpression implements InvokableRule
9+
{
10+
/**
11+
* Validates a string cron expression is correct
12+
*
13+
* @param string $attribute
14+
* @param mixed $value
15+
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
16+
* @return void
17+
*/
18+
public function __invoke($attribute, $value, $fail)
19+
{
20+
$is_valid = CronExpression::isValidExpression($value);
21+
22+
if (! $is_valid) {
23+
$fail('Cron expression is not valid');
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)