From 54b5493f21a682978eb0aff7f8078f7b3a2dcb89 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Sat, 6 Apr 2019 12:25:38 +0200 Subject: [PATCH] Add `IFormDocument::(get|has)Button()` See #2509 --- .../form/builder/DialogFormDocument.class.php | 2 +- .../form/builder/FormDocument.class.php | 20 ++++++++++++++++++- .../form/builder/IFormDocument.class.php | 18 +++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/wcfsetup/install/files/lib/system/form/builder/DialogFormDocument.class.php b/wcfsetup/install/files/lib/system/form/builder/DialogFormDocument.class.php index f9937a92f9..e4b2b90ecb 100644 --- a/wcfsetup/install/files/lib/system/form/builder/DialogFormDocument.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/DialogFormDocument.class.php @@ -51,7 +51,7 @@ class DialogFormDocument extends FormDocument { protected function createDefaultButton() { parent::createDefaultButton(); - $this->getButtons()['submitButton']->attribute('data-type', 'submit'); + $this->getButton('submitButton')->attribute('data-type', 'submit'); if ($this->isCancelable()) { $this->addButton( diff --git a/wcfsetup/install/files/lib/system/form/builder/FormDocument.class.php b/wcfsetup/install/files/lib/system/form/builder/FormDocument.class.php index b6416492d7..9116846ef5 100644 --- a/wcfsetup/install/files/lib/system/form/builder/FormDocument.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/FormDocument.class.php @@ -299,13 +299,24 @@ class FormDocument implements IFormDocument { return $this->action; } + /** + * @inheritDoc + */ + public function getButton($buttonId) { + if (!$this->hasButton($buttonId)) { + throw new \InvalidArgumentException("Unknown button with id '{$buttonId}'."); + } + + return $this->buttons[$buttonId]; + } + /** * @inheritDoc */ public function getButtons() { return $this->buttons; } - + /** * @inheritDoc */ @@ -438,6 +449,13 @@ class FormDocument implements IFormDocument { return $this->successMessage; } + /** + * @inheritDoc + */ + public function hasButton($buttonId) { + return isset($this->buttons[$buttonId]); + } + /** * @inheritDoc */ diff --git a/wcfsetup/install/files/lib/system/form/builder/IFormDocument.class.php b/wcfsetup/install/files/lib/system/form/builder/IFormDocument.class.php index 8da4e81567..0a9df5b0c1 100644 --- a/wcfsetup/install/files/lib/system/form/builder/IFormDocument.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/IFormDocument.class.php @@ -111,6 +111,16 @@ interface IFormDocument extends IFormParentNode { */ public function getAction(); + /** + * Returns the button with the given id. + * + * @param string $buttonId id of requested button + * @return IFormButton + * + * @throws \InvalidArgumentException if no such button exists + */ + public function getButton($buttonId); + /** * Returns the buttons registered for this form document. * @@ -210,6 +220,14 @@ interface IFormDocument extends IFormParentNode { */ public function getSuccessMessage(); + /** + * Returns `true` if a button with the given id exists and `false` otherwise. + * + * @param string $buttonId id of checked button + * @return boolean + */ + public function hasButton($buttonId); + /** * Returns `true` if the default button is added to the form during in the `build()` method * and `false` otherwise. -- 2.20.1