Skip to content

Commit

Permalink
Allow setting a decorator on a form
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed May 10, 2019
1 parent 9e2dace commit e86459d
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 13 deletions.
5 changes: 2 additions & 3 deletions src/DataHandlers/AbstractDataHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}
2 changes: 2 additions & 0 deletions src/DataHandlers/Interfaces/DataHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,6 @@ public function addError(ValidationException ...$errors);
* @return DataHandler
*/
public function clearErrors();

public function render(): string;
}
8 changes: 7 additions & 1 deletion src/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public function getSubmitDecorator(): ?Decorator

public function render(): string
{
return (string)$this->produceSafeHTML();
return $this->produceSafeHTML()->getContent();
}

public function produceSafeHTML(): SafeHtml
Expand All @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion tests/Decorators/FormCustomDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"><div class="hidden"><input type="hidden"', $output);
Expand Down
6 changes: 0 additions & 6 deletions tests/Supporting/CustomDecoratorForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use Packaged\Form\Csrf\CsrfForm;
use Packaged\Form\DataHandlers\TextDataHandler;
use Packaged\Form\Form\Interfaces\FormDecorator;

class CustomDecoratorForm extends CsrfForm
{
Expand All @@ -16,9 +15,4 @@ protected function _initDataHandlers()
$this->name = new TextDataHandler();
$this->email = new TextDataHandler();
}

protected function _defaultDecorator(): FormDecorator
{
return new CustomFormDecorator();
}
}
4 changes: 2 additions & 2 deletions tests/Supporting/CustomFormDecorator.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ use Packaged\Tests\Form\Supporting\CustomFormDecorator;
$form = $this->getForm();
?>
<div class="hidden"><?= implode('', $this->_getHiddenDecorators()); ?></div>
<?= $form->name; ?>
<?= $form->email; ?>
<?= $form->name->render(); ?>
<?= $form->email->render(); ?>
<?= $this->getForm()->getSubmitDecorator(); ?>

0 comments on commit e86459d

Please sign in to comment.