From b3356d0bb5bdef8f8e0d7f5daa03fd5d68aaacba Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Thu, 4 Jul 2019 10:42:25 +0200 Subject: [PATCH] DefaultFormRenderer: Fix GET Application\UI\Form (#222) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Forms\Form allows to pass stringable objects to setAction method, which is also used by Application\UI\Form to store Application\UI\Link object. DefaultFormRenderer calls string manipulation functions on the Form’s action when rendering, causing it to raise a TypeError when strict types are enabled. To fix this, I am stringifying the action in the DefaultFormRenderer. I have opted to do it there, rather than in the Form::setAction method, in case the Link object is not ready yet at the time the Form is created. --- src/Forms/Rendering/DefaultFormRenderer.php | 1 + tests/Forms/Forms.objectAction.phpt | 22 +++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 tests/Forms/Forms.objectAction.phpt diff --git a/src/Forms/Rendering/DefaultFormRenderer.php b/src/Forms/Rendering/DefaultFormRenderer.php index a9910c361..a5fa6aeb5 100644 --- a/src/Forms/Rendering/DefaultFormRenderer.php +++ b/src/Forms/Rendering/DefaultFormRenderer.php @@ -167,6 +167,7 @@ public function renderBegin(): string if ($this->form->isMethod('get')) { $el = clone $this->form->getElementPrototype(); + $el->action = (string) $el->action; $query = parse_url($el->action, PHP_URL_QUERY) ?: ''; $el->action = str_replace("?$query", '', $el->action); $s = ''; diff --git a/tests/Forms/Forms.objectAction.phpt b/tests/Forms/Forms.objectAction.phpt new file mode 100644 index 000000000..c05bf5f03 --- /dev/null +++ b/tests/Forms/Forms.objectAction.phpt @@ -0,0 +1,22 @@ +setMethod($form::GET); + $form->setAction(new class { + public function __toString(): string + { + return '/some/link'; + } + }); + + echo $form; +});