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">