diff --git a/library/Icinga/Web/Form.php b/library/Icinga/Web/Form.php index 2fc54b997..2357cddbc 100644 --- a/library/Icinga/Web/Form.php +++ b/library/Icinga/Web/Form.php @@ -94,6 +94,22 @@ abstract class Form extends \Zend_Form */ abstract protected function create(); + /** + * Method called before validation + */ + protected function preValid(array $data) + { + } + + /** + * Method called after validation + * @param array $data + * @param bool &$isValid + */ + protected function postValid(array $data, &$isValid) + { + } + /** * Setter for request * @param \Zend_Controller_Request_Abstract $request The request object of a session @@ -139,20 +155,24 @@ abstract class Form extends \Zend_Form */ public function isValid($data) { - $check = null; + $checkData = null; // Elements must be there to validate $this->buildForm(); if ($data === null) { - $check = $this->getRequest()->getParams(); + $checkData = $this->getRequest()->getParams(); } elseif ($data instanceof \Zend_Controller_Request_Abstract) { - $check = $data->getParams(); + $checkData = $data->getParams(); } else { - $check = $data; + $checkData = $data; } - return parent::isValid($check); + $this->preValid($checkData); + $checkValue = parent::isValid($checkData); + $this->postValid($checkData, $checkValue); + + return $checkValue; } /** diff --git a/modules/monitoring/application/controllers/CommandController.php b/modules/monitoring/application/controllers/CommandController.php index 8617e504e..68b2fcecf 100644 --- a/modules/monitoring/application/controllers/CommandController.php +++ b/modules/monitoring/application/controllers/CommandController.php @@ -1,4 +1,6 @@ form = $form; + } + + /** + * Getter for form + * @return Form + */ + public function getForm() + { + return $this->form; + } + + /** + * Test if we have a valid form object + * @return bool + */ + public function issetForm() + { + return $this->getForm() !== null && ($this->getForm() instanceof Form); + } + + /** + * Post dispatch method + * + * When we have a form put it into the view + */ + public function postDispatch() + { + if ($this->issetForm()) { + if ($this->getRequest()->isPost() && $this->getForm()->isValid(null) === true) { + $this->_helper->viewRenderer->setNoRender(true); + $this->_helper->layout()->disableLayout(); + } + $this->view->form = $this->getForm(); + } + + parent::postDispatch(); + } + /** * Controller configuration @@ -66,9 +123,6 @@ class Monitoring_CommandController extends ModuleActionController public function init() { if ($this->_request->isPost()) { - // Save time and memory. We're only working on post - $this->_helper->viewRenderer->setNoRender(true); - $this->_helper->layout()->disableLayout(); $instance = $this->_request->getPost("instance"); @@ -152,9 +206,9 @@ class Monitoring_CommandController extends ModuleActionController $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Disable active checks')); $form->addNote(t('Disable active checks for this object.')); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -169,9 +223,9 @@ class Monitoring_CommandController extends ModuleActionController $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Enable active checks')); $form->addNote(t('Enable active checks for this object.')); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -185,9 +239,9 @@ class Monitoring_CommandController extends ModuleActionController $form = new RescheduleNextCheck(); $form->setRequest($this->getRequest()); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -204,9 +258,9 @@ class Monitoring_CommandController extends ModuleActionController $form->setRequest($this->getRequest()); $form->setType($type); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -221,9 +275,9 @@ class Monitoring_CommandController extends ModuleActionController $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Stop obsessing')); $form->addNote(t('Stop obsessing over this object.')); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -238,9 +292,9 @@ class Monitoring_CommandController extends ModuleActionController $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Start obsessing')); $form->addNote(t('Start obsessing over this object.')); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -255,9 +309,9 @@ class Monitoring_CommandController extends ModuleActionController $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Stop accepting passive checks')); $form->addNote(t('Passive checks for this object will be omitted.')); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -272,9 +326,9 @@ class Monitoring_CommandController extends ModuleActionController $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Start accepting passive checks')); $form->addNote(t('Passive checks for this object will be accepted.')); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -289,9 +343,9 @@ class Monitoring_CommandController extends ModuleActionController $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Disable notifications')); $form->addNote(t('Notifications for this object will be disabled.')); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -306,9 +360,9 @@ class Monitoring_CommandController extends ModuleActionController $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Enable notifications')); $form->addNote(t('Notifications for this object will be enabled.')); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -321,9 +375,9 @@ class Monitoring_CommandController extends ModuleActionController { $form = new CustomNotification(); $form->setRequest($this->getRequest()); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -337,9 +391,9 @@ class Monitoring_CommandController extends ModuleActionController $form = new ScheduleDowntime(); $form->setRequest($this->getRequest()); $form->setWithChildren(false); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -353,9 +407,9 @@ class Monitoring_CommandController extends ModuleActionController $form = new ScheduleDowntime(); $form->setRequest($this->getRequest()); $form->setWithChildren(true); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -370,9 +424,9 @@ class Monitoring_CommandController extends ModuleActionController $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Remove downtime(s)')); $form->addNote(t('Remove downtime(s) from this host and its services.')); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -387,9 +441,9 @@ class Monitoring_CommandController extends ModuleActionController $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Disable notifications')); $form->addNote(t('Notifications for this host and its services will be disabled.')); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -404,9 +458,9 @@ class Monitoring_CommandController extends ModuleActionController $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Enable notifications')); $form->addNote(t('Notifications for this host and its services will be enabled.')); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -422,9 +476,9 @@ class Monitoring_CommandController extends ModuleActionController $form->setWithChildren(true); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -439,9 +493,9 @@ class Monitoring_CommandController extends ModuleActionController $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Disable active checks')); $form->addNote(t('Disable active checks for this host and its services.')); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -456,9 +510,9 @@ class Monitoring_CommandController extends ModuleActionController $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Enable active checks')); $form->addNote(t('Enable active checks for this host and its services.')); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -473,9 +527,9 @@ class Monitoring_CommandController extends ModuleActionController $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Disable event handler')); $form->addNote(t('Disable event handler for this object.')); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -490,9 +544,9 @@ class Monitoring_CommandController extends ModuleActionController $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Enable event handler')); $form->addNote(t('Enable event handler for this object.')); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -507,9 +561,9 @@ class Monitoring_CommandController extends ModuleActionController $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Disable flapping detection')); $form->addNote(t('Disable flapping detection for this object.')); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -524,9 +578,9 @@ class Monitoring_CommandController extends ModuleActionController $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Enable flapping detection')); $form->addNote(t('Enable flapping detection for this object.')); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -540,9 +594,9 @@ class Monitoring_CommandController extends ModuleActionController $form = new CommentForm(); $form->setRequest($this->getRequest()); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -557,9 +611,9 @@ class Monitoring_CommandController extends ModuleActionController $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Reset attributes')); $form->addNote(t('Reset modified attributes to its default.')); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -573,9 +627,9 @@ class Monitoring_CommandController extends ModuleActionController $form = new Acknowledge(); $form->setRequest($this->getRequest()); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -590,9 +644,9 @@ class Monitoring_CommandController extends ModuleActionController $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Remove problem acknowledgement')); $form->addNote(t('Remove problem acknowledgement for this object.')); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -606,9 +660,9 @@ class Monitoring_CommandController extends ModuleActionController $form = new DelayNotification(); $form->setRequest($this->getRequest()); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } @@ -627,10 +681,12 @@ class Monitoring_CommandController extends ModuleActionController $form->setFieldLabel(t('Downtime id')); $form->addNote(t('Delete a single downtime with the id shown above')); - $this->view->form = $form; + $this->setForm($form); - if ($form->isValid(null) && $this->getRequest()->isPost()) { + if ($this->getRequest()->isPost() && $form->isValid(null)) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } } + +// @codingStandardsIgnoreStop \ No newline at end of file diff --git a/modules/monitoring/application/forms/Command/AbstractCommand.php b/modules/monitoring/application/forms/Command/AbstractCommand.php index 6b7549e06..7991ab4f2 100644 --- a/modules/monitoring/application/forms/Command/AbstractCommand.php +++ b/modules/monitoring/application/forms/Command/AbstractCommand.php @@ -31,12 +31,23 @@ namespace Monitoring\Form\Command; use Icinga\Web\Form; use Icinga\Web\Form\Element\Note; use \Zend_Form_Element_Hidden; +use \Zend_Validate_Date; /** * Class AbstractCommand */ abstract class AbstractCommand extends Form { + /** + * Default date format + */ + const DEFAULT_DATE_FORMAT = 'Y-m-d H:i:s'; + + /** + * Default format for validation + */ + const DEFAULT_DATE_VALIDATION = 'yyyy-MM-dd hh:ii:ss'; + /** * Label for submit button * @@ -196,9 +207,10 @@ abstract class AbstractCommand extends Form $authorField = new Zend_Form_Element_Hidden( array( - 'name' => 'author', - 'value' => $authorName, - 'label' => t('Author name') + 'name' => 'author', + 'label' => t('Author name'), + 'value' => $authorName, + 'required' => true ) ); @@ -213,4 +225,34 @@ abstract class AbstractCommand extends Form return $authorField; } + + /** + * Getter for date format + * TODO(mh): Should be user preferences + * @return string + */ + protected function getDateFormat() + { + return self::DEFAULT_DATE_FORMAT; + } + + /** + * Getter for date validation format + * @return string + */ + protected function getDateValidationFormat() + { + return self::DEFAULT_DATE_VALIDATION; + } + + /** + * Create a new date validator + * @return Zend_Validate_Date + */ + protected function createDateTimeValidator() + { + $validator = new Zend_Validate_Date(); + $validator->setFormat($this->getDateValidationFormat()); + return $validator; + } } diff --git a/modules/monitoring/application/forms/Command/Acknowledge.php b/modules/monitoring/application/forms/Command/Acknowledge.php index 7e6863366..cbd345c55 100644 --- a/modules/monitoring/application/forms/Command/Acknowledge.php +++ b/modules/monitoring/application/forms/Command/Acknowledge.php @@ -50,8 +50,9 @@ class Acknowledge extends AbstractCommand 'textarea', 'comment', array( - 'label' => t('Comment'), - 'rows' => 4 + 'label' => t('Comment'), + 'rows' => 4, + 'required' => true ) ); @@ -68,8 +69,7 @@ class Acknowledge extends AbstractCommand 'checkbox', 'expire', array( - 'label' => t('Use expire time'), - 'value' => false + 'label' => t('Use expire time') ) ); @@ -81,7 +81,7 @@ class Acknowledge extends AbstractCommand array( 'name' => 'expiretime', 'label' => t('Expire time'), - 'value' => $now->format('Y-m-d H:i:s') + 'value' => $now->format($this->getDateFormat()) ) ); @@ -128,4 +128,17 @@ class Acknowledge extends AbstractCommand parent::create(); } + + /** + * Add validator for dependent fields + * @param array $data + */ + protected function preValid(array $data) + { + if (isset($data['expire']) && $data['expire'] === '1') { + $expireTime = $this->getElement('expiretime'); + $expireTime->setRequired(true); + $expireTime->addValidator($this->createDateTimeValidator(), true); + } + } } diff --git a/modules/monitoring/application/forms/Command/Comment.php b/modules/monitoring/application/forms/Command/Comment.php index 7c265582d..961fb66db 100644 --- a/modules/monitoring/application/forms/Command/Comment.php +++ b/modules/monitoring/application/forms/Command/Comment.php @@ -42,8 +42,9 @@ class Comment extends AbstractCommand 'textarea', 'comment', array( - 'label' => t('Comment'), - 'rows' => 4 + 'label' => t('Comment'), + 'rows' => 4, + 'required' => true ) ); diff --git a/modules/monitoring/application/forms/Command/Confirmation.php b/modules/monitoring/application/forms/Command/Confirmation.php index 0f555da2b..abea66669 100644 --- a/modules/monitoring/application/forms/Command/Confirmation.php +++ b/modules/monitoring/application/forms/Command/Confirmation.php @@ -31,7 +31,7 @@ namespace Monitoring\Form\Command; /** * Simple confirmation form * - * Exist to make the abstract form concrete. + * Exist to make the abstract available as concrete class */ class Confirmation extends AbstractCommand { diff --git a/modules/monitoring/application/forms/Command/ConfirmationWithIdentifier.php b/modules/monitoring/application/forms/Command/ConfirmationWithIdentifier.php index 94b5e5c75..e877af941 100644 --- a/modules/monitoring/application/forms/Command/ConfirmationWithIdentifier.php +++ b/modules/monitoring/application/forms/Command/ConfirmationWithIdentifier.php @@ -96,6 +96,13 @@ class ConfirmationWithIdentifier extends Confirmation $fieldLabel = $this->getFieldLabel(); $hiddenField = new Zend_Form_Element_Hidden($this->getFieldName()); + $hiddenField->setValue($value); + $hiddenField->setRequired(true); + $hiddenField->addValidator( + 'digits', + true + ); + $hiddenField->removeDecorator('Label'); $hiddenField->addDecorator( diff --git a/modules/monitoring/application/forms/Command/CustomNotification.php b/modules/monitoring/application/forms/Command/CustomNotification.php index 0cf1a344b..70f56eb9c 100644 --- a/modules/monitoring/application/forms/Command/CustomNotification.php +++ b/modules/monitoring/application/forms/Command/CustomNotification.php @@ -47,8 +47,9 @@ class CustomNotification extends AbstractCommand 'textarea', 'comment', array( - 'label' => t('Comment'), - 'rows' => 4 + 'label' => t('Comment'), + 'rows' => 4, + 'required' => true ) ); diff --git a/modules/monitoring/application/forms/Command/DelayNotification.php b/modules/monitoring/application/forms/Command/DelayNotification.php index 5af1ccc13..deb1d007b 100644 --- a/modules/monitoring/application/forms/Command/DelayNotification.php +++ b/modules/monitoring/application/forms/Command/DelayNotification.php @@ -33,6 +33,10 @@ namespace Monitoring\Form\Command; */ class DelayNotification extends AbstractCommand { + /** + * Biggest value for minutes + */ + const MAX_VALUE = 1440; /** * Interface method to build the form * @see Form::create() @@ -43,9 +47,20 @@ class DelayNotification extends AbstractCommand 'text', 'minutes', array( - 'label' => t('Notification delay'), - 'style' => 'width: 80px;', - 'value' => 0 + 'label' => t('Notification delay'), + 'style' => 'width: 80px;', + 'value' => 0, + 'required' => true, + 'validators' => array( + array( + 'between', + true, + array( + 'min' => 1, + 'max' => self::MAX_VALUE + ) + ) + ) ) ); diff --git a/modules/monitoring/application/forms/Command/RescheduleNextCheck.php b/modules/monitoring/application/forms/Command/RescheduleNextCheck.php index fbc48f1b1..bec8f8c0e 100644 --- a/modules/monitoring/application/forms/Command/RescheduleNextCheck.php +++ b/modules/monitoring/application/forms/Command/RescheduleNextCheck.php @@ -54,6 +54,9 @@ class RescheduleNextCheck extends WithChildrenCommand ) ); + $dateElement->setRequired(true); + $dateElement->addValidator($this->createDateTimeValidator(), true); + $this->addElement($dateElement); $checkBox = new Zend_Form_Element_Checkbox( diff --git a/modules/monitoring/application/forms/Command/ScheduleDowntime.php b/modules/monitoring/application/forms/Command/ScheduleDowntime.php index 0bf006de6..c0ef55736 100644 --- a/modules/monitoring/application/forms/Command/ScheduleDowntime.php +++ b/modules/monitoring/application/forms/Command/ScheduleDowntime.php @@ -32,6 +32,8 @@ use Icinga\Web\Form\Element\DateTime; use \DateTime as PhpDateTime; use \DateInterval; use \Zend_Form_Element_Text; +use \Zend_Validate_GreaterThan; +use \Zend_Validate_Digits; /** * Form for any ScheduleDowntime command @@ -44,12 +46,6 @@ class ScheduleDowntime extends WithChildrenCommand */ const DEFAULT_ENDTIME_INTERVAL = 'PT1H'; - /** - * Default time format - * TODO(mh): Should be configurable on a central place (#4424) - */ - const DEFAULT_DATE_FORMAT = 'Y-m-d H:i:s'; - /** * Type constant for fixed downtimes */ @@ -69,11 +65,11 @@ class ScheduleDowntime extends WithChildrenCommand $out = array(); $dateTimeObject = new PhpDateTime(); - $out[] = $dateTimeObject->format(self::DEFAULT_DATE_FORMAT); + $out[] = $dateTimeObject->format($this->getDateFormat()); $dateInterval = new DateInterval(self::DEFAULT_ENDTIME_INTERVAL); $dateTimeObject->add($dateInterval); - $out[] = $dateTimeObject->format(self::DEFAULT_DATE_FORMAT); + $out[] = $dateTimeObject->format($this->getDateFormat()); return $out; } @@ -102,8 +98,9 @@ class ScheduleDowntime extends WithChildrenCommand 'textarea', 'comment', array( - 'label' => t('Comment'), - 'rows' => 4 + 'label' => t('Comment'), + 'rows' => 4, + 'required' => true ) ); @@ -111,7 +108,22 @@ class ScheduleDowntime extends WithChildrenCommand 'text', 'triggered', array( - 'label' => t('Triggered by') + 'label' => t('Triggered by'), + 'value' => 0, + 'required' => true, + 'validators' => array( + array( + 'Digits', + true + ), + array( + 'GreaterThan', + true, + array( + 'min' => -1 + ) + ) + ) ) ); @@ -124,6 +136,8 @@ class ScheduleDowntime extends WithChildrenCommand 'value' => $timestampStart ) ); + $dateTimeStart->setRequired(true); + $dateTimeStart->addValidator($this->createDateTimeValidator(), true); $dateTimeEnd = new DateTime( array( @@ -132,6 +146,8 @@ class ScheduleDowntime extends WithChildrenCommand 'value' => $timestampEnd ) ); + $dateTimeEnd->setRequired(true); + $dateTimeEnd->addValidator($this->createDateTimeValidator(), true); $this->addElements(array($dateTimeStart, $dateTimeEnd)); @@ -140,7 +156,17 @@ class ScheduleDowntime extends WithChildrenCommand 'type', array( 'label' => t('Downtime type'), - 'multiOptions' => $this->getDowntimeTypeOptions() + 'multiOptions' => $this->getDowntimeTypeOptions(), + 'required' => true, + 'validators' => array( + array( + 'InArray', + true, + array( + array_keys($this->getDowntimeTypeOptions()) + ) + ) + ) ) ); @@ -188,6 +214,19 @@ class ScheduleDowntime extends WithChildrenCommand 0 => t('Do nothing with child objects'), 1 => t('Schedule triggered downtime for all child objects'), 2 => t('Schedule non-triggered downtime for all child objects') + ), + 'validators' => array( + array( + 'Digits', + true + ), + array( + 'InArray', + true, + array( + array(0, 1, 2) + ) + ) ) ) ); @@ -199,4 +238,29 @@ class ScheduleDowntime extends WithChildrenCommand parent::create(); } + + /** + * Change validators at runtime + * @param array $data + */ + protected function preValid(array $data) + { + /* + * Other values needed when downtime type change to flexible + */ + if (isset($data['type']) && $data['type'] === self::TYPE_FLEXIBLE) { + $greaterThanValidator = new Zend_Validate_GreaterThan(-1); + $digitsValidator = new Zend_Validate_Digits(); + + $hours = $this->getElement('hours'); + $hours->setRequired(true); + $hours->addValidator($digitsValidator, true); + $hours->addValidator($greaterThanValidator, true); + + $minutes = $this->getElement('minutes'); + $minutes->setRequired(true); + $minutes->addValidator($digitsValidator, true); + $minutes->addValidator($greaterThanValidator, true); + } + } } diff --git a/modules/monitoring/application/forms/Command/SubmitPassiveCheckResult.php b/modules/monitoring/application/forms/Command/SubmitPassiveCheckResult.php index 676388451..122babfbf 100644 --- a/modules/monitoring/application/forms/Command/SubmitPassiveCheckResult.php +++ b/modules/monitoring/application/forms/Command/SubmitPassiveCheckResult.php @@ -122,7 +122,21 @@ class SubmitPassiveCheckResult extends AbstractCommand 'pluginstate', array( 'label' => t('Plugin state'), - 'multiOptions' => $this->getOptions() + 'multiOptions' => $this->getOptions(), + 'required' => true, + 'validators' => array( + array( + 'Digits', + true + ), + array( + 'InArray', + true, + array( + array_keys($this->getOptions()) + ) + ) + ) ) ); @@ -130,8 +144,9 @@ class SubmitPassiveCheckResult extends AbstractCommand 'textarea', 'checkoutput', array( - 'label' => t('Check output'), - 'rows' => 2 + 'label' => t('Check output'), + 'rows' => 2, + 'required' => true ) );