Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextBase support mixed as value #178

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Forms/Controls/TextBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Nette;
use Nette\Forms\Form;
use Nette\Forms\ScalarValue;
use Nette\Utils\Strings;


Expand Down Expand Up @@ -41,7 +42,7 @@ public function setValue($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));
}
$this->value = $value;
$this->value = $value instanceof ScalarValue ? $value->getMixedValue() : $value;
$this->rawValue = (string) $value;
return $this;
}
Expand Down
43 changes: 43 additions & 0 deletions src/Forms/ScalarValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Nette\Forms;


class ScalarValue
{

/** @var string */
private $scalarValue;

/** @var mixed */
private $mixedValue;


public function __construct(string $scalarValue, $mixedValue)
{
$this->scalarValue = $scalarValue;
$this->mixedValue = $mixedValue;
}


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


public function __toString(): string
{
return $this->scalarValue;
}
}
28 changes: 28 additions & 0 deletions tests/Forms/Controls.BaseControl.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
declare(strict_types=1);

use Nette\Forms\Form;
use Nette\Forms\ScalarValue;
use Nette\Forms\Validator;
use Tester\Assert;

Expand Down Expand Up @@ -145,3 +146,30 @@ test(function () { // disabled & submitted

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


test(function () { // scalarValue
$form = new Form;
$input = $form->addText('text');
$input->setValue(new ScalarValue('scalarValue', null));

Assert::null($input->getValue());
Assert::same('<input type="text" name="text" id="frm-text" value="scalarValue">', (string) $input->getControl());
});


test(function () { // scalarValue from filter
$form = new Form;

$input = $form->addText('text');
$input->setValue('scalarValue');
$input->addFilter(function (string $scalarValue): ScalarValue {
return new ScalarValue($scalarValue, 'mixedValue');
});

$form->validate();

Assert::same('mixedValue', $input->getValue());
Assert::same('<input type="text" name="text" id="frm-text" value="scalarValue">', (string) $input->getControl());
Assert::true(Validator::validateFilled($input));
});