Skip to content

Commit 06c51d9

Browse files
committed
added Rules::addFilter()
1 parent 8871974 commit 06c51d9

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

src/Forms/Controls/TextBase.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,8 @@ public function setMaxLength($length)
9696
*/
9797
public function addFilter($filter)
9898
{
99-
Nette\Utils\Callback::check($filter);
100-
return parent::addRule(function($control) use ($filter) {
101-
$control->setValue( call_user_func($filter, $control->getValue()) );
102-
return TRUE;
103-
});
99+
$this->rules->addFilter($filter);
100+
return $this;
104101
}
105102

106103

src/Forms/Rules.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,24 @@ public function endCondition()
151151
}
152152

153153

154+
/**
155+
* Adds a filter callback.
156+
* @param callable
157+
* @return self
158+
*/
159+
public function addFilter($filter)
160+
{
161+
Nette\Utils\Callback::check($filter);
162+
$this->rules[] = $rule = new Rule;
163+
$rule->control = $this->control;
164+
$rule->validator = function($control) use ($filter) {
165+
$control->setValue( call_user_func($filter, $control->getValue()) );
166+
return TRUE;
167+
};
168+
return $this;
169+
}
170+
171+
154172
/**
155173
* Toggles HTML element visibility.
156174
* @param string element id

tests/Forms/Controls.TextBase.loadData.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,17 @@ test(function() { // filter
173173
$input->validate();
174174
Assert::same( 'olleh', $input->getValue() );
175175
});
176+
177+
178+
test(function() { // filter in condition
179+
$_POST = array('text' => 'hello');
180+
181+
$form = new Form;
182+
$input = $form->addText('text');
183+
$input->addCondition($form::FILLED)
184+
->addFilter('strrev');
185+
186+
Assert::same( 'hello', $input->getValue() );
187+
$input->validate();
188+
Assert::same( 'olleh', $input->getValue() );
189+
});

0 commit comments

Comments
 (0)