Skip to content
This repository was archived by the owner on Sep 13, 2024. It is now read-only.

Commit f4f9d74

Browse files
authored
Merge pull request #2 from VladimirBerdnik/master
Add 'sometimes' rule modifier
2 parents af85731 + b9d1413 commit f4f9d74

File tree

7 files changed

+24
-4
lines changed

7 files changed

+24
-4
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes History
22

3+
1.0.4
4+
-----
5+
Add 'sometimes' rule modifier
6+
37
1.0.3
48
-----
59
Add ServiceProvider - allows to use rule builder without explicit ->toString()

src/DatabaseRuleSet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct(array $rules = [])
3232
* @return GenericRuleSet
3333
* @see \Illuminate\Validation\Rules\Exists
3434
*/
35-
public function exists($table, $column = 'NULL', \Closure $callback = null): GenericRuleSet
35+
public function exists(string $table, string $column = 'NULL', \Closure $callback = null): GenericRuleSet
3636
{
3737
$rule = \Illuminate\Validation\Rule::exists($table, $column);
3838
if ($callback !== null) {

src/RequiredRules.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function requiredWithoutAll(string ...$otherFields)
6363
}
6464

6565
/**
66-
* The field under validation must be present and not empty if the anotherfield field is equal to any value.
66+
* The field under validation must be present and not empty if the anotherField field is equal to any value.
6767
* @param string $anotherField
6868
* @param mixed $value
6969
* @return static
@@ -74,7 +74,7 @@ public function requiredIf(string $anotherField, $value)
7474
}
7575

7676
/**
77-
* The field under validation must be present and not empty unless the anotherfield field is equal to any value.
77+
* The field under validation must be present and not empty unless the anotherField field is equal to any value.
7878
* @param string $anotherField
7979
* @param mixed $value
8080
* @return static

src/Rule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* @method static GenericRuleSet min($minValue) The field under validation must have a minimum value. Strings, numerics, arrays, and files are evaluated in the same fashion as the size rule.
2525
* @method static GenericRuleSet max($maxValue) The field under validation must be less than or equal to a maximum value. Strings, numerics, arrays, and files are evaluated in the same fashion as the size rule.
2626
* @method static GenericRuleSet inArray(string $anotherField) The field under validation must exist in $anotherField's values.
27+
* @method static GenericRuleSet sometimes() Run validation checks against a field only if that field is present in the input array.
2728
* @method static GenericRuleSet requiredWith(string ...$otherFields) This field is required, if another field has value
2829
* @method static GenericRuleSet requiredWithAll(string ...$otherFields) The field under validation must be present and not empty only if all of the other specified fields are present.
2930
* @method static GenericRuleSet requiredWithout(string ...$otherFields) This field is required, if another field has no value

src/RuleSet.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class RuleSet implements IRule
4949
'max',
5050
'between',
5151
'size',
52+
'sometimes',
5253
];
5354

5455
/** @var array */

src/SimpleRules.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
trait SimpleRules
99
{
1010
/**
11-
* The field under validation must exist in anotherfield's values.
11+
* The field under validation must exist in anotherField's values.
1212
* @param string $anotherFiled
1313
* @return static
1414
*/
@@ -117,4 +117,13 @@ public function size(int $value)
117117
{
118118
return $this->appendIfNotExists("size:$value");
119119
}
120+
121+
/**
122+
* Run validation checks against a field only if that field is present in the input array.
123+
* @return static
124+
*/
125+
public function sometimes()
126+
{
127+
return $this->appendIfNotExists("sometimes");
128+
}
120129
}

tests/RuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ public function testSize()
5454
$this->assertEquals('size:20', Rule::size(20));
5555
}
5656

57+
public function testSometimes()
58+
{
59+
$this->assertEquals('sometimes', Rule::sometimes());
60+
}
61+
5762
/**
5863
* Rule 'in' can accept values as list of parameters or as array
5964
*/

0 commit comments

Comments
 (0)