From e86459df534c37a4295ff44acfbe47585e0758be Mon Sep 17 00:00:00 2001 From: Brooke Bryan Date: Fri, 10 May 2019 11:25:42 +0100 Subject: [PATCH] Allow setting a decorator on a form --- src/DataHandlers/AbstractDataHandler.php | 5 ++--- src/DataHandlers/Interfaces/DataHandler.php | 2 ++ src/Form/Form.php | 8 +++++++- tests/Decorators/FormCustomDecoratorTest.php | 4 +++- tests/Supporting/CustomDecoratorForm.php | 6 ------ tests/Supporting/CustomFormDecorator.phtml | 4 ++-- 6 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/DataHandlers/AbstractDataHandler.php b/src/DataHandlers/AbstractDataHandler.php index d732786..9752d08 100644 --- a/src/DataHandlers/AbstractDataHandler.php +++ b/src/DataHandlers/AbstractDataHandler.php @@ -294,9 +294,8 @@ public function setDefaultValue($defaultValue) return $this; } - public function __toString() + public function render(): string { - return $this->getDecorator()->produceSafeHTML()->getContent(); + return $this->getDecorator()->render(); } - } diff --git a/src/DataHandlers/Interfaces/DataHandler.php b/src/DataHandlers/Interfaces/DataHandler.php index de49ce5..bb315aa 100644 --- a/src/DataHandlers/Interfaces/DataHandler.php +++ b/src/DataHandlers/Interfaces/DataHandler.php @@ -97,4 +97,6 @@ public function addError(ValidationException ...$errors); * @return DataHandler */ public function clearErrors(); + + public function render(): string; } diff --git a/src/Form/Form.php b/src/Form/Form.php index 00e86af..fdf8646 100644 --- a/src/Form/Form.php +++ b/src/Form/Form.php @@ -207,7 +207,7 @@ public function getSubmitDecorator(): ?Decorator public function render(): string { - return (string)$this->produceSafeHTML(); + return $this->produceSafeHTML()->getContent(); } public function produceSafeHTML(): SafeHtml @@ -224,6 +224,12 @@ public function getDecorator(): FormDecorator return $this->_decorator->setForm($this); } + public function setDecorator(FormDecorator $decorator) + { + $this->_decorator = $decorator; + return $this; + } + protected function _defaultDecorator(): FormDecorator { return new DefaultFormDecorator(); diff --git a/tests/Decorators/FormCustomDecoratorTest.php b/tests/Decorators/FormCustomDecoratorTest.php index 3bd973c..344b59a 100644 --- a/tests/Decorators/FormCustomDecoratorTest.php +++ b/tests/Decorators/FormCustomDecoratorTest.php @@ -3,13 +3,15 @@ namespace Packaged\Tests\Form\Decorators; use Packaged\Tests\Form\Supporting\CustomDecoratorForm; +use Packaged\Tests\Form\Supporting\CustomFormDecorator; use PHPUnit\Framework\TestCase; class FormCustomDecoratorTest extends TestCase { public function testCustomDecorator() { - $form = new CustomDecoratorForm('abc'); + $form = new CustomDecoratorForm('csrf'); + $form->setDecorator(new CustomFormDecorator()); $output = $form->produceSafeHTML()->getContent(); $this->assertContains('name="email"', $output); $this->assertContains('method="post">