Skip to content
This repository has been archived by the owner on Nov 15, 2020. It is now read-only.

Commit

Permalink
Add support for attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrtak-CZ committed Mar 20, 2015
1 parent da3b40e commit 8407ab0
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Nella/Forms/DateTime/DateInput.php
Expand Up @@ -30,6 +30,9 @@ class DateInput extends \Nette\Forms\Controls\BaseControl
/** @var string */
private $format;

/** @var mixed[]|array */
private $attributes = array();

/**
* @param string
* @param string|NULL
Expand Down
66 changes: 66 additions & 0 deletions src/Nella/Forms/DateTime/DateTimeInput.php
Expand Up @@ -47,6 +47,12 @@ class DateTimeInput extends \Nette\Forms\Controls\BaseControl
/** @var string */
private $time;

/** @var mixed[]|array */
private $dateAttributes = array();

/** @var mixed[]|array */
private $timeAttributes = array();

/**
* @param string
* @param string
Expand Down Expand Up @@ -118,11 +124,18 @@ public function loadHttpData()
$this->time = $this->getHttpData(Form::DATA_LINE, '[' . static::NAME_TIME . ']');
}

/**
* @return string
*/
public function getControl()
{
return $this->getControlPart(static::NAME_DATE) . $this->getControlPart(static::NAME_TIME);
}

/**
* @param string $key
* @return \Nette\Utils\Html
*/
public function getControlPart($key)
{
$name = $this->getHtmlName();
Expand All @@ -137,6 +150,10 @@ public function getControlPart($key)
$control->disabled($this->disabled);
}

foreach ($this->dateAttributes as $name => $value) {
$control->$name = $value;
}

return $control;
} elseif ($key === static::NAME_TIME) {
$control = \Nette\Utils\Html::el('input')->name($name . '[' . static::NAME_TIME . ']');
Expand All @@ -148,6 +165,10 @@ public function getControlPart($key)
$control->disabled($this->disabled);
}

foreach ($this->timeAttributes as $name => $value) {
$control->$name = $value;
}

return $control;
}

Expand All @@ -168,6 +189,51 @@ public function validateDateTime(DateTimeInput $dateTimeInput)
return $this->isDisabled() || !$this->isFilled() || $this->getValue() !== NULL;
}

/**
* @param string $name
* @param mixed $value
* @return \Nella\Forms\DateTime\DateTimeInput
*/
public function setDateAttribute($name, $value = TRUE)
{
if ($value === NULL) {
unset($this->dateAttributes[$name]);
} else {
$this->dateAttributes[$name] = $value;
}

return $this;
}

/**
* @param string $name
* @param mixed $value
* @return \Nella\Forms\DateTime\DateTimeInput
*/
public function setTimeAttribute($name, $value = TRUE)
{
if ($value === NULL) {
unset($this->timeAttributes[$name]);
} else {
$this->timeAttributes[$name] = $value;
}

return $this;
}

/**
* @param string $name
* @param mixed $value
* @return \Nella\Forms\DateTime\DateTimeInput
*/
public function setAttribute($name, $value = TRUE)
{
$this->setDateAttribute($name, $value);
$this->setTimeAttribute($name, $value);

return $this;
}

public static function register()
{
if (static::$registered) {
Expand Down
9 changes: 9 additions & 0 deletions tests/Nella/Forms/DateTime/DateInputTest.phpt
Expand Up @@ -154,6 +154,15 @@ class DateInputTest extends \Tester\TestCase
Assert::equal(array('test'), $control->getErrors());
}

public function testAttribute()
{
$control = $this->createControl();

$control->setAttribute('readonly', 'readonly');

Assert::equal('readonly', $control->getControl()->readonly);
}

/**
* @throws \Nette\InvalidStateException
*/
Expand Down
30 changes: 30 additions & 0 deletions tests/Nella/Forms/DateTime/DateTimeInputTest.phpt
Expand Up @@ -186,6 +186,36 @@ class DateTimeInputTest extends \Tester\TestCase
Assert::equal(array('test'), $control->getErrors());
}

public function testAttribute()
{
$control = $this->createControl();

$control->setAttribute('readonly', 'readonly');

Assert::equal('readonly', $control->getControlPart(DateTimeInput::NAME_DATE)->readonly);
Assert::equal('readonly', $control->getControlPart(DateTimeInput::NAME_TIME)->readonly);
}

public function testDateAttribute()
{
$control = $this->createControl();

$control->setDateAttribute('readonly', 'readonly');

Assert::equal('readonly', $control->getControlPart(DateTimeInput::NAME_DATE)->readonly);
Assert::false(isset($control->getControlPart(DateTimeInput::NAME_TIME)->readonly));
}

public function testTimeAttribute()
{
$control = $this->createControl();

$control->setTimeAttribute('readonly', 'readonly');

Assert::equal('readonly', $control->getControlPart(DateTimeInput::NAME_TIME)->readonly);
Assert::false(isset($control->getControlPart(DateTimeInput::NAME_DATE)->readonly));
}

/**
* @throws \Nette\InvalidStateException
*/
Expand Down

0 comments on commit 8407ab0

Please sign in to comment.