Skip to content

Commit e58f331

Browse files
committed
TextBase, TextInput, TextArea: refactoring, rendered value is built in getRenderedValue()
1 parent ffaeebe commit e58f331

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

src/Forms/Controls/TextArea.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,8 @@ public function __construct($label = NULL)
3333
*/
3434
public function getControl()
3535
{
36-
$value = $this->getValue();
37-
if ($value === '') {
38-
$value = $this->translate($this->emptyValue);
39-
}
4036
return parent::getControl()
41-
->setText($value);
37+
->setText($this->getRenderedValue());
4238
}
4339

4440
}

src/Forms/Controls/TextBase.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,17 @@ public function getControl()
111111
}
112112

113113

114+
/**
115+
* @return string|NULL
116+
*/
117+
protected function getRenderedValue()
118+
{
119+
return $this->rawValue === ''
120+
? ($this->emptyValue === '' ? NULL : $this->translate($this->emptyValue))
121+
: $this->rawValue;
122+
}
123+
124+
114125
public function addRule($validator, $message = NULL, $arg = NULL)
115126
{
116127
if ($validator === Form::LENGTH || $validator === Form::MAX_LENGTH) {

src/Forms/Controls/TextInput.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,10 @@ public function setType($type)
5757
*/
5858
public function getControl()
5959
{
60-
$input = parent::getControl();
61-
if ($input->type !== 'password' && ($this->rawValue !== '' || $this->emptyValue !== '')) {
62-
$input->value = $this->rawValue === ''
63-
? $this->translate($this->emptyValue)
64-
: $this->rawValue;
65-
}
66-
$input->type = $input->type ?: 'text';
67-
return $input;
60+
return parent::getControl()->addAttributes([
61+
'value' => $this->control->type === 'password' ? $this->control->value : $this->getRenderedValue(),
62+
'type' => $this->control->type ?: 'text',
63+
]);
6864
}
6965

7066

tests/Forms/Controls.TextArea.render.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,12 @@ test(function () { // rendering options
9797
$input->getControl();
9898
Assert::true($input->getOption('rendered'));
9999
});
100+
101+
102+
test(function () { // setEmptyValue
103+
$form = new Form;
104+
$input = $form->addTextArea('text')
105+
->setEmptyValue('empty ');
106+
107+
Assert::same('<textarea name="text" id="frm-text" data-nette-empty-value="empty">empty </textarea>', (string) $input->getControl());
108+
});

0 commit comments

Comments
 (0)