From 4f7b56ade194ec19ef38af4792f8c36776b31126 Mon Sep 17 00:00:00 2001 From: Ravi Srinivasa Date: Tue, 30 May 2023 12:49:22 +0200 Subject: [PATCH] Prevent downtime flexible duration being set to `0h0m` --- .../Object/ScheduleServiceDowntimeForm.php | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/application/forms/Command/Object/ScheduleServiceDowntimeForm.php b/application/forms/Command/Object/ScheduleServiceDowntimeForm.php index 05453e24..c7c97827 100644 --- a/application/forms/Command/Object/ScheduleServiceDowntimeForm.php +++ b/application/forms/Command/Object/ScheduleServiceDowntimeForm.php @@ -179,10 +179,22 @@ class ScheduleServiceDowntimeForm extends CommandForm 'number', 'hours', [ - 'required' => true, - 'label' => t('Duration'), - 'value' => $this->flexibleDuration->h, - 'min' => 0 + 'required' => true, + 'label' => t('Duration'), + 'value' => $this->flexibleDuration->h, + 'min' => 0, + 'validators' => [ + 'Callback' => function ($value, $validator) { + /** @var CallbackValidator $validator */ + + if ($this->getValue('minutes') == 0 && $value == 0) { + $validator->addMessage(t('The duration must not be zero')); + return false; + } + + return true; + } + ] ] ); $this->registerElement($hoursInput); @@ -202,9 +214,15 @@ class ScheduleServiceDowntimeForm extends CommandForm new HtmlElement('label', null, new HtmlElement('span', null, Text::create(t('Minutes')))) ); - $hoursInput->getWrapper() - ->add($minutesInput) - ->getAttributes()->add('class', 'downtime-duration'); + $hoursInput->getWrapper()->on( + IcingaFormDecorator::ON_ASSEMBLED, + function ($hoursInputWrapper) use ($minutesInput, $hoursInput) { + $hoursInputWrapper + ->insertAfter($minutesInput, $hoursInput) + ->getAttributes()->add('class', 'downtime-duration'); + } + ); + $hoursInput->prependWrapper( new HtmlElement('label', null, new HtmlElement('span', null, Text::create(t('Hours')))) );