Skip to content

Commit

Permalink
Allow IDs to be set on DataHandlers
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Nov 5, 2019
1 parent be2679e commit f2ede82
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/DataHandlers/AbstractDataHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

abstract class AbstractDataHandler implements DataHandler
{
protected $_id;
protected $_name;
protected $_value;
protected $_label;
Expand Down Expand Up @@ -239,6 +240,25 @@ public function setDecorator(DataHandlerDecorator $decorator)

abstract protected function _defaultDecorator(): DataHandlerDecorator;

/**
* @return mixed
*/
public function getId()
{
return $this->_id;
}

/**
* @param mixed $id
*
* @return AbstractDataHandler
*/
public function setId($id)
{
$this->_id = $id;
return $this;
}

/**
* @return mixed
*/
Expand Down
14 changes: 13 additions & 1 deletion src/Decorators/AbstractDataHandlerDecorator.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Packaged\Form\Decorators;

use Packaged\Form\DataHandlers\AbstractDataHandler;
use Packaged\Form\DataHandlers\Interfaces\DataHandler;
use Packaged\Form\Decorators\Interfaces\DataHandlerDecorator;
use Packaged\Glimpse\Core\HtmlTag;
Expand All @@ -22,7 +23,7 @@ abstract class AbstractDataHandlerDecorator extends AbstractDecorator implements
{
protected $_tag = 'div';
/**
* @var DataHandler
* @var DataHandler|AbstractDataHandler
*/
protected $_handler;

Expand Down Expand Up @@ -96,13 +97,24 @@ protected function _getContentForRender()
protected function _configureInputElement(HtmlElement $input)
{
$id = $input->getId();
if(empty($id) && $this->_handler instanceof AbstractDataHandler)
{
$id = $this->_handler->getId();
}

$name = $this->_handler->getName();

if(empty($id) && $this->_label && $name)
{
// create an id
$id = strtolower(str_replace(' ', '-', Strings::splitOnCamelCase($name)) . '-' . Strings::randomString(3));
}

if(!empty($id))
{
$input->setId($id);
}

$input->setAttribute('name', $name, true);
}

Expand Down
1 change: 1 addition & 0 deletions tests/Decorators/FormCustomDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public function testCustomDecorator()
$output = $form->produceSafeHTML()->getContent();
$this->assertContains('name="email"', $output);
$this->assertContains('method="post"><div class="hidden"><input type="hidden"', $output);
$this->assertContains('id="name-input"', $output);
}
}
1 change: 1 addition & 0 deletions tests/Supporting/CustomDecoratorForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ protected function _initDataHandlers()
{
parent::_initDataHandlers();
$this->name = new TextDataHandler();
$this->name->setId("name-input");
$this->email = new TextDataHandler();
}
}

0 comments on commit f2ede82

Please sign in to comment.