Prohibits on Checkbox not working

I have form with a checkbox and a date picker. Neither are required but both should never be fillable/submittable together. I am using the prohibits rule so one blocks the other.
Forms\Components\Checkbox::make('start_immediately')
->prohibits('start_date'),

Forms\Components\DatePicker::make('start_date')
->prefixIcon('heroicon-o-calendar')
->minDate(now())
->native(false)
->prohibits('start_immediately'),
Forms\Components\Checkbox::make('start_immediately')
->prohibits('start_date'),

Forms\Components\DatePicker::make('start_date')
->prefixIcon('heroicon-o-calendar')
->minDate(now())
->native(false)
->prohibits('start_immediately'),
Leaving the fields blank works, only ticking start_immeditately works, however selecting a start_date but leaving start_immediately blank throws a validation error as shown in the image as the start_immeditately field is present even if it’s value is false. Is there are way around this?
No description
Solution:
I can use a custom rule like so which fixes this. I think it’s the only way to make it work as the built-in rules don’t always play well with checkboxes ```php Forms\Components\Checkbox::make('start_immediately') ->rules([...
Jump to solution
1 Reply
Solution
Ric Le Poidevin
Ric Le Poidevin9mo ago
I can use a custom rule like so which fixes this. I think it’s the only way to make it work as the built-in rules don’t always play well with checkboxes
Forms\Components\Checkbox::make('start_immediately')
->rules([
function(Get $get) {
return function (string $attribute, $value, Closure $fail) use ($get) {
if ($value && $get('start_date')) {
$fail('Please select either start immediately, a start date or leave both blank.');
}
};
}
]),
Forms\Components\DatePicker::make('start_date')
->prefixIcon('heroicon-o-calendar')
->minDate(now())
->native(false),
Forms\Components\Checkbox::make('start_immediately')
->rules([
function(Get $get) {
return function (string $attribute, $value, Closure $fail) use ($get) {
if ($value && $get('start_date')) {
$fail('Please select either start immediately, a start date or leave both blank.');
}
};
}
]),
Forms\Components\DatePicker::make('start_date')
->prefixIcon('heroicon-o-calendar')
->minDate(now())
->native(false),
Want results from more Discord servers?
Add your server