Skip to content

Commit

Permalink
added Rules::addFilter()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 18, 2015
1 parent 8871974 commit 06c51d9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/Forms/Controls/TextBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,8 @@ public function setMaxLength($length)
*/
public function addFilter($filter)
{
Nette\Utils\Callback::check($filter);
return parent::addRule(function($control) use ($filter) {
$control->setValue( call_user_func($filter, $control->getValue()) );
return TRUE;
});
$this->rules->addFilter($filter);
return $this;
}


Expand Down
18 changes: 18 additions & 0 deletions src/Forms/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,24 @@ public function endCondition()
}


/**
* Adds a filter callback.
* @param callable
* @return self
*/
public function addFilter($filter)
{
Nette\Utils\Callback::check($filter);
$this->rules[] = $rule = new Rule;
$rule->control = $this->control;
$rule->validator = function($control) use ($filter) {
$control->setValue( call_user_func($filter, $control->getValue()) );
return TRUE;
};
return $this;
}


/**
* Toggles HTML element visibility.
* @param string element id
Expand Down
14 changes: 14 additions & 0 deletions tests/Forms/Controls.TextBase.loadData.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,17 @@ test(function() { // filter
$input->validate();
Assert::same( 'olleh', $input->getValue() );
});


test(function() { // filter in condition
$_POST = array('text' => 'hello');

$form = new Form;
$input = $form->addText('text');
$input->addCondition($form::FILLED)
->addFilter('strrev');

Assert::same( 'hello', $input->getValue() );
$input->validate();
Assert::same( 'olleh', $input->getValue() );
});

0 comments on commit 06c51d9

Please sign in to comment.