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

Added "when" method for validation at condition #55

Merged
merged 25 commits into from Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
9512580
Fix camelcase method name in tests [skip ci]
thenotsoft Jan 27, 2020
996d413
Merge branch 'master' of https://github.com/yiisoft/validator
thenotsoft Feb 24, 2020
fe9bbd7
translator implementation and refactoring ...
thenotsoft Feb 25, 2020
4e32844
translator implementation & refactoring in progress...
thenotsoft Feb 25, 2020
9cc2a53
minor refactoring
thenotsoft Feb 25, 2020
8e2d266
minor refactoring
thenotsoft Feb 25, 2020
dde6cd7
moved message translation to rule
thenotsoft Feb 26, 2020
7f1f51d
add default translation domain
thenotsoft Feb 26, 2020
5e2df48
minor refactoring
thenotsoft Feb 26, 2020
9225b2c
added ability to translate callback message
thenotsoft Feb 26, 2020
953393d
add fallback formatMessage
thenotsoft Feb 26, 2020
41213a6
change to private "formatMessage"
thenotsoft Feb 26, 2020
a67aacb
minor fix
thenotsoft Feb 26, 2020
0498ad6
fix
thenotsoft Feb 26, 2020
e465868
Merge branch 'master' of https://github.com/yiisoft/validator
thenotsoft Feb 27, 2020
d6e7987
implement skipOnError ability & minor refactoring
thenotsoft Feb 27, 2020
4c3b41d
minor changes
thenotsoft Feb 27, 2020
deb3464
added "when" method for validation at condition
thenotsoft Feb 27, 2020
d6176ad
Merge branch 'master' into new_feature
thenotsoft Feb 28, 2020
b3b06a5
removed unnecessary test
thenotsoft Mar 2, 2020
a04db48
Update src/Rule.php
thenotsoft Mar 2, 2020
1d378f7
Merge branch 'master' into new_feature
samdark Mar 3, 2020
c7e89d8
hasErrors() wasn't used anywhere
samdark Mar 3, 2020
f73e4db
Remove unused import
samdark Mar 3, 2020
2b6ea60
Add phpdoc
samdark Mar 3, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/Rule.php
Expand Up @@ -17,6 +17,11 @@ abstract class Rule
private bool $skipOnEmpty = false;
private bool $skipOnError = true;

/**
* @var callable|null
*/
private $when = null;
samdark marked this conversation as resolved.
Show resolved Hide resolved

/**
* Validates the value
*
Expand All @@ -31,7 +36,10 @@ final public function validate($value, DataSetInterface $dataSet = null, bool $p
return new Result();
}

if ($this->skipOnError && $previousRulesErrored) {
if (
($this->skipOnError && $previousRulesErrored) ||
(is_callable($this->when) && !call_user_func($this->when, $value, $dataSet))
) {
return new Result();
}

Expand Down Expand Up @@ -82,6 +90,32 @@ public function translateMessage(string $message, array $arguments = []): string
);
}

/**
* Add a PHP callable whose return value determines whether this rule should be applied.
* By default rule will be always applied.
*
* The signature of the callable should be `function ($value, DataSetInterface $dataSet): bool`, where $value and $dataSet
* refer to the value validated and the data set in which context it is validated. The callable should return
* a boolean value.
*
* The following example will enable the validator only when the country currently selected is USA:
*
* ```php
* function ($value, DataSetInterface $dataSet) {
return $dataSet->getAttributeValue('country') === Country::USA;
}
* ```
*
* @param callable $callback
* @return $this
*/
public function when(callable $callback): self
{
$new = clone $this;
$new->when = $callback;
return $new;
}

public function skipOnError(bool $value): self
{
$new = clone $this;
Expand Down
16 changes: 16 additions & 0 deletions tests/RulesTest.php
Expand Up @@ -56,6 +56,22 @@ static function ($value): Result {
$this->assertCount(1, $result->getErrors());
}

public function testWhenValidate()
{
$rules = new Rules(
[
(new Number())->min(10),
(new Number())->min(10)->when(fn() => false)->skipOnError(false),
(new Number())->min(10)->skipOnError(false)
]
);

$result = $rules->validate(1);

$this->assertFalse($result->isValid());
$this->assertCount(2, $result->getErrors());
}

public function testSkipOnError()
{
$rules = new Rules(
Expand Down