Skip to content

Commit

Permalink
HiddenField support addFilter and nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
MilanPala committed Nov 7, 2017
1 parent 92e516c commit 37edcdb
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 9 deletions.
50 changes: 46 additions & 4 deletions src/Forms/Controls/HiddenField.php
Expand Up @@ -17,9 +17,15 @@
*/
class HiddenField extends BaseControl
{
/** @var mixed unfiltered submitted value */
protected $rawValue = '';

/** @var bool */
private $persistValue;

/** @var bool */
private $nullable;


public function __construct($persistentValue = null)
{
Expand All @@ -29,7 +35,8 @@ public function __construct($persistentValue = null)
if ($persistentValue !== null) {
$this->unmonitor(Nette\Forms\Form::class);
$this->persistValue = true;
$this->value = (string) $persistentValue;
$this->value = $persistentValue;
$this->rawValue = (string) $persistentValue;
}
}

Expand All @@ -41,16 +48,51 @@ public function __construct($persistentValue = null)
*/
public function setValue($value)
{
if (!is_scalar($value) && $value !== null && !method_exists($value, '__toString')) {
if ($value === null) {
$value = '';
} elseif (!is_scalar($value) && !method_exists($value, '__toString')) {
throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or null, %s given in field '%s'.", gettype($value), $this->name));
}
if (!$this->persistValue) {
$this->value = (string) $value;
$this->rawValue = (string) $value;
$this->value = $value;
}
return $this;
}


/**
* Returns control's value.
* @return mixed
*/
public function getValue()
{
return $this->nullable && $this->value === '' ? null : $this->value;
}


/**
* Sets whether getValue() returns NULL instead of empty string.
* @return static
*/
public function setNullable(bool $value = true)
{
$this->nullable = $value;
return $this;
}


/**
* Appends input string filter callback.
* @return static
*/
public function addFilter(callable $filter)
{
$this->getRules()->addFilter($filter);
return $this;
}


/**
* Generates control's HTML element.
*/
Expand All @@ -61,7 +103,7 @@ public function getControl(): Nette\Utils\Html
return $el->addAttributes([
'name' => $this->getHtmlName(),
'disabled' => $this->isDisabled(),
'value' => $this->value,
'value' => $this->rawValue,
]);
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Forms/Controls.CsrfProtection.phpt
Expand Up @@ -32,15 +32,15 @@ Assert::same('hidden', $input->getOption('type'));
$input->setValue(null);
Assert::false(CsrfProtection::validateCsrf($input));

call_user_func([$input, 'Nette\Forms\Controls\BaseControl::setValue'], '12345678901234567890123456789012345678');
call_user_func([$input, 'Nette\Forms\Controls\HiddenField::setValue'], '12345678901234567890123456789012345678');
Assert::false(CsrfProtection::validateCsrf($input));

$value = $input->getControl()->value;
call_user_func([$input, 'Nette\Forms\Controls\BaseControl::setValue'], $value);
call_user_func([$input, 'Nette\Forms\Controls\HiddenField::setValue'], $value);
Assert::true(CsrfProtection::validateCsrf($input));

session_regenerate_id();
call_user_func([$input, 'Nette\Forms\Controls\BaseControl::setValue'], $value);
call_user_func([$input, 'Nette\Forms\Controls\HiddenField::setValue'], $value);
Assert::false(CsrfProtection::validateCsrf($input));


Expand Down
48 changes: 46 additions & 2 deletions tests/Forms/Controls.HiddenField.loadData.phpt
Expand Up @@ -68,9 +68,36 @@ test(function () { // setValue() and invalid argument
test(function () { // object
$form = new Form;
$input = $form->addHidden('hidden')
->setValue(new Nette\Utils\DateTime('2013-07-05'));
->setValue($data = new Nette\Utils\DateTime('2013-07-05'));

Assert::same('2013-07-05 00:00:00', $input->getValue());
Assert::same($data, $input->getValue());
});


test(function () { // object from string by filter
$date = new Nette\Utils\DateTime('2013-07-05');
$_POST = ['text' => (string) $date];
$form = new Form;
$input = $form->addHidden('text');
$input->addFilter(function($value) {
return $value ? new \Nette\Utils\DateTime($value) : $value;
});

Assert::same((string) $date, $input->getValue());
$input->validate();
Assert::equal($date, $input->getValue());
});


test(function () { // int from string
$_POST = ['text' => '10'];
$form = new Form;
$input = $form->addHidden('text');
$input->addRule($form::INTEGER);

Assert::same('10', $input->getValue());
$input->validate();
Assert::equal(10, $input->getValue());
});


Expand All @@ -81,3 +108,20 @@ test(function () { // persistent

Assert::same('persistent', $input->getValue());
});


test(function () { // nullable
$form = new Form;
$input = $form['hidden'] = new Nette\Forms\Controls\HiddenField();
$input->setNullable();
Assert::null($input->getValue());
});


test(function () { // nullable
$form = new Form;
$input = $form['hidden'] = new Nette\Forms\Controls\HiddenField();
$input->setValue(null);
$input->setNullable();
Assert::null($input->getValue());
});

0 comments on commit 37edcdb

Please sign in to comment.