From fc2352fb1e1a5c4f7ed5d89c01a13e3fc3435fe9 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Wed, 24 Jun 2015 14:54:14 +0200 Subject: [PATCH] QuickForm: improve submission detection Still not complete --- .../forms/IcingaCommandArgumentForm.php | 1 - application/forms/IcingaCommandForm.php | 2 - application/forms/IcingaEndpointForm.php | 2 - application/forms/IcingaHostForm.php | 2 - application/forms/IcingaHostGroupForm.php | 2 - .../forms/IcingaHostGroupMemberForm.php | 2 - application/forms/IcingaHostVarForm.php | 3 -- application/forms/IcingaServiceForm.php | 2 - application/forms/IcingaServiceGroupForm.php | 2 - .../forms/IcingaServiceGroupMemberForm.php | 2 - application/forms/IcingaServiceVarForm.php | 2 - application/forms/IcingaTimePeriodForm.php | 2 - application/forms/IcingaUserForm.php | 2 - application/forms/IcingaUserGroupForm.php | 2 - .../forms/IcingaUserGroupMemberForm.php | 2 - application/forms/IcingaZoneForm.php | 2 - library/Director/Web/Form/QuickForm.php | 42 ++++++++++++++++++- 17 files changed, 41 insertions(+), 33 deletions(-) diff --git a/application/forms/IcingaCommandArgumentForm.php b/application/forms/IcingaCommandArgumentForm.php index b83cbad3..19eec1bf 100644 --- a/application/forms/IcingaCommandArgumentForm.php +++ b/application/forms/IcingaCommandArgumentForm.php @@ -47,7 +47,6 @@ class IcingaCommandArgumentForm extends DirectorObjectForm $this->translate('Whether this is a mandatory parameter') ); */ - $this->addElement('submit', $this->translate('Store')); } protected function addCustomVariable($varname) diff --git a/application/forms/IcingaCommandForm.php b/application/forms/IcingaCommandForm.php index c9ec7a9e..16a616b1 100644 --- a/application/forms/IcingaCommandForm.php +++ b/application/forms/IcingaCommandForm.php @@ -50,7 +50,5 @@ class IcingaCommandForm extends DirectorObjectForm 'template' => $this->translate('Command template'), )) )); - - $this->addElement('submit', $this->translate('Store')); } } diff --git a/application/forms/IcingaEndpointForm.php b/application/forms/IcingaEndpointForm.php index 15dcbbc6..78e0b374 100644 --- a/application/forms/IcingaEndpointForm.php +++ b/application/forms/IcingaEndpointForm.php @@ -53,7 +53,5 @@ class IcingaEndpointForm extends DirectorObjectForm 'description' => $this->translate('Check this host in this specific Icinga cluster zone'), 'required' => true )); - - $this->addElement('submit', $this->translate('Store')); } } diff --git a/application/forms/IcingaHostForm.php b/application/forms/IcingaHostForm.php index 54ae8b50..05042360 100644 --- a/application/forms/IcingaHostForm.php +++ b/application/forms/IcingaHostForm.php @@ -94,7 +94,5 @@ class IcingaHostForm extends DirectorObjectForm 'label' => $this->translate('Cluster Zone'), 'description' => $this->translate('Check this host in this specific Icinga cluster zone') )); - - $this->addElement('submit', $this->translate('Store')); } } diff --git a/application/forms/IcingaHostGroupForm.php b/application/forms/IcingaHostGroupForm.php index adacbfc6..fb56e192 100644 --- a/application/forms/IcingaHostGroupForm.php +++ b/application/forms/IcingaHostGroupForm.php @@ -37,7 +37,5 @@ class IcingaHostGroupForm extends DirectorObjectForm 'label' => $this->translate('Display Name'), 'description' => $this->translate('The name which should displayed.') )); - - $this->addElement('submit', $this->translate('Store')); } } diff --git a/application/forms/IcingaHostGroupMemberForm.php b/application/forms/IcingaHostGroupMemberForm.php index 145defc8..d8647a70 100644 --- a/application/forms/IcingaHostGroupMemberForm.php +++ b/application/forms/IcingaHostGroupMemberForm.php @@ -20,7 +20,5 @@ class IcingaHostGroupMemberForm extends DirectorObjectForm 'label' => $this->translate('Host'), 'description' => $this->translate('The name of the host') )); - - $this->addElement('submit', $this->translate('Store')); } } diff --git a/application/forms/IcingaHostVarForm.php b/application/forms/IcingaHostVarForm.php index f3bbaf9b..3fe0a70d 100644 --- a/application/forms/IcingaHostVarForm.php +++ b/application/forms/IcingaHostVarForm.php @@ -11,7 +11,6 @@ class IcingaHostVarForm extends DirectorObjectForm { public function setup() { - $this->addElement('select', 'host_id', array( 'label' => $this->translate('Host'), 'description' => $this->translate('The name of the host'), @@ -32,7 +31,5 @@ class IcingaHostVarForm extends DirectorObjectForm 'label' => $this->translate('Format'), 'description' => $this->translate('value format') )); - - $this->addElement('submit', $this->translate('Store')); } } diff --git a/application/forms/IcingaServiceForm.php b/application/forms/IcingaServiceForm.php index 7c945996..764c3c03 100644 --- a/application/forms/IcingaServiceForm.php +++ b/application/forms/IcingaServiceForm.php @@ -84,7 +84,5 @@ class IcingaServiceForm extends DirectorObjectForm 'label' => $this->translate('Servicegroups'), 'description' => $this->translate('One or more comma separated servicegroup names') )); - - $this->addElement('submit', $this->translate('Store')); } } diff --git a/application/forms/IcingaServiceGroupForm.php b/application/forms/IcingaServiceGroupForm.php index 3758e11f..68e35eed 100644 --- a/application/forms/IcingaServiceGroupForm.php +++ b/application/forms/IcingaServiceGroupForm.php @@ -37,7 +37,5 @@ class IcingaServiceGroupForm extends DirectorObjectForm 'label' => $this->translate('Display Name'), 'description' => $this->translate('The name which should displayed.') )); - - $this->addElement('submit', $this->translate('Store')); } } diff --git a/application/forms/IcingaServiceGroupMemberForm.php b/application/forms/IcingaServiceGroupMemberForm.php index 6f82de50..bfdbee55 100644 --- a/application/forms/IcingaServiceGroupMemberForm.php +++ b/application/forms/IcingaServiceGroupMemberForm.php @@ -20,7 +20,5 @@ class IcingaServiceGroupMemberForm extends DirectorObjectForm 'label' => $this->translate('Service'), 'description' => $this->translate('The name of the service') )); - - $this->addElement('submit', $this->translate('Store')); } } diff --git a/application/forms/IcingaServiceVarForm.php b/application/forms/IcingaServiceVarForm.php index 7d19eebc..e0fcb74d 100644 --- a/application/forms/IcingaServiceVarForm.php +++ b/application/forms/IcingaServiceVarForm.php @@ -31,7 +31,5 @@ class IcingaServiceVarForm extends DirectorObjectForm 'label' => $this->translate('Format'), 'description' => $this->translate('value format') )); - - $this->addElement('submit', $this->translate('Store')); } } diff --git a/application/forms/IcingaTimePeriodForm.php b/application/forms/IcingaTimePeriodForm.php index 068e940d..a5ce5bbb 100644 --- a/application/forms/IcingaTimePeriodForm.php +++ b/application/forms/IcingaTimePeriodForm.php @@ -48,7 +48,5 @@ class IcingaTimePeriodForm extends DirectorObjectForm 'description' => $this->translate('Check this host in this specific Icinga cluster zone'), 'required' => true )); - - $this->addElement('submit', $this->translate('Store')); } } diff --git a/application/forms/IcingaUserForm.php b/application/forms/IcingaUserForm.php index d928007b..fcd18231 100644 --- a/application/forms/IcingaUserForm.php +++ b/application/forms/IcingaUserForm.php @@ -59,7 +59,5 @@ class IcingaUserForm extends DirectorObjectForm 'label' => $this->translate('Usergroups'), 'description' => $this->translate('One or more comma separated usergroup names') )); - - $this->addElement('submit', $this->translate('Store')); } } diff --git a/application/forms/IcingaUserGroupForm.php b/application/forms/IcingaUserGroupForm.php index a3dff7e1..90c18add 100644 --- a/application/forms/IcingaUserGroupForm.php +++ b/application/forms/IcingaUserGroupForm.php @@ -42,7 +42,5 @@ class IcingaUserGroupForm extends DirectorObjectForm 'label' => $this->translate('Cluster Zone'), 'description' => $this->translate('Check this usergroup in this specific Icinga cluster zone') )); - - $this->addElement('submit', $this->translate('Store')); } } diff --git a/application/forms/IcingaUserGroupMemberForm.php b/application/forms/IcingaUserGroupMemberForm.php index ff142632..d25b2b6a 100644 --- a/application/forms/IcingaUserGroupMemberForm.php +++ b/application/forms/IcingaUserGroupMemberForm.php @@ -20,7 +20,5 @@ class IcingaUserGroupMemberForm extends DirectorObjectForm 'label' => $this->translate('User'), 'description' => $this->translate('The name of the user') )); - - $this->addElement('submit', $this->translate('Store')); } } diff --git a/application/forms/IcingaZoneForm.php b/application/forms/IcingaZoneForm.php index 4a3d2995..bad34539 100644 --- a/application/forms/IcingaZoneForm.php +++ b/application/forms/IcingaZoneForm.php @@ -37,7 +37,5 @@ class IcingaZoneForm extends DirectorObjectForm 'label' => $this->translate('Parent Zone'), 'description' => $this->translate('Chose an (optional) parent zone') )); - - $this->addElement('submit', $this->translate('Store')); } } diff --git a/library/Director/Web/Form/QuickForm.php b/library/Director/Web/Form/QuickForm.php index 209fdb87..20d54dc9 100644 --- a/library/Director/Web/Form/QuickForm.php +++ b/library/Director/Web/Form/QuickForm.php @@ -46,6 +46,8 @@ abstract class QuickForm extends Zend_Form protected $successMessage; + protected $submitLabel; + public function __construct($options = null) { parent::__construct($options); @@ -55,6 +57,14 @@ abstract class QuickForm extends Zend_Form $this->regenerateCsrfToken(); $this->setup(); $this->onSetup(); + $this->addSubmitButtonIfSet(); + } + + protected function addSubmitButtonIfSet() + { + if (false !== ($label = $this->getSubmitLabel())) { + $this->addElement('submit', $label); + } } protected function createIdElement() @@ -64,6 +74,21 @@ abstract class QuickForm extends Zend_Form $this->getElement(self::ID)->setIgnore(true); } + public function getSubmitLabel() + { + if ($this->submitLabel === null) { + return $this->translate('Submit'); + } + + return $this->submitLabel; + } + + public function setSubmitLabel($label) + { + $this->submitLabel = $label; + return $this; + } + public function regenerateCsrfToken() { if (! $element = $this->getElement(self::CSRF)) { @@ -114,7 +139,22 @@ abstract class QuickForm extends Zend_Form public function hasBeenSubmitted() { - return $this->hasBeenSent(); + if ($this->hasBeenSubmitted === null) { + $req = $this->getRequest(); + if ($req->isPost()) { + $post = $req->getPost(); + $label = $this->getSubmitLabel(); + if ($label === false) { + $this->hasBeenSubmitted = $this->hasBeenSent(); + } + $this->hasBeenSubmitted = array_key_exists($label, $post) && + $post[$label] === $label; + } else { + $this->hasBeenSubmitted === false; + } + } + + return $this->hasBeenSubmitted; } protected function beforeValidation($data = array())