From ccc809853a32b42dbeed41446ba4394a86cb9476 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 24 Jul 2015 13:46:17 +0200 Subject: [PATCH] UserBackendConfigForm: Allow to manually validate the configuration refs #7588 --- .../forms/Config/UserBackendConfigForm.php | 83 +++++++++++++++++++ public/css/icinga/main-content.less | 9 ++ 2 files changed, 92 insertions(+) diff --git a/application/forms/Config/UserBackendConfigForm.php b/application/forms/Config/UserBackendConfigForm.php index d811cc16b..5ee4674bb 100644 --- a/application/forms/Config/UserBackendConfigForm.php +++ b/application/forms/Config/UserBackendConfigForm.php @@ -44,6 +44,7 @@ class UserBackendConfigForm extends ConfigForm { $this->setName('form_config_authbackend'); $this->setSubmitLabel($this->translate('Save Changes')); + $this->setValidatePartial(true); } /** @@ -399,4 +400,86 @@ class UserBackendConfigForm extends ConfigForm ) ); } + + /** + * Run the configured backend's inspection checks and show the result, if necessary + * + * This will only run any validation if the user pushed the 'backend_validation' button. + * + * @param array $formData + * + * @return bool + */ + public function isValidPartial(array $formData) + { + if ($this->getElement('backend_validation')->isChecked() && parent::isValid($formData)) { + $inspection = static::inspectUserBackend($this); + if ($inspection !== null) { + $join = function ($e) use (& $join) { + return is_string($e) ? $e : join("\n", array_map($join, $e)); + }; + $this->addElement( + 'note', + 'inspection_output', + array( + 'order' => 0, + 'value' => '' . $this->translate('Validation Log') . "\n\n" + . join("\n", array_map($join, $inspection->toArray())), + 'decorators' => array( + 'ViewHelper', + array('HtmlTag', array('tag' => 'pre', 'class' => 'log-output')), + ) + ) + ); + + if ($inspection->hasError()) { + $this->warning(sprintf( + $this->translate('Failed to successfully validate the configuration: %s'), + $inspection->getError() + )); + return false; + } + } + + $this->info($this->translate('The configuration has been successfully validated.')); + } + + return true; + } + + /** + * Add a submit button to this form and one to manually validate the configuration + * + * Calls parent::addSubmitButton() to add the submit button. + * + * @return $this + */ + public function addSubmitButton() + { + parent::addSubmitButton() + ->getElement('btn_submit') + ->setDecorators(array('ViewHelper')); + + $this->addElement( + 'submit', + 'backend_validation', + array( + 'ignore' => true, + 'label' => $this->translate('Validate Configuration'), + 'decorators' => array('ViewHelper') + ) + ); + $this->addDisplayGroup( + array('btn_submit', 'backend_validation'), + 'submit_validation', + array( + 'decorators' => array( + 'FormElements', + array('HtmlTag', array('tag' => 'div', 'class' => 'control-group')) + ) + ) + ); + + return $this; + } } diff --git a/public/css/icinga/main-content.less b/public/css/icinga/main-content.less index c51ef8722..d0f182913 100644 --- a/public/css/icinga/main-content.less +++ b/public/css/icinga/main-content.less @@ -12,6 +12,15 @@ p code { padding: 0.3em; } +pre.log-output { + padding: 0.5em; + margin-top: 0; + margin-bottom: 2em; + max-height: 12em; + overflow: auto; + border: 1px solid #ddd; +} + a { color: #39a; }