Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 52 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ $v->validate();
```

## requiredWith fields usage
The `requiredWith` rule checks that the field is required, not null, and not the empty string, if any other fields are present, not null, and not the empty string.
The `requiredWith` rule checks that the field is required, not null, and not an empty string, if any other fields are present, not null, not boolean false, and not an empty string.
```php
// password field will be required when the username field is provided and not empty
$v->rule('requiredWith', 'password', 'username');
Expand Down Expand Up @@ -290,7 +290,7 @@ $v->validate();
```

### Strict flag
The strict flag will change the `requiredWith` rule to `requiredWithAll` which will require the field only if ALL of the other fields are present, not null, and not the empty string.
The strict flag will change the `requiredWith` rule to `requiredWithAll` which will require the field only if all the other fields are present, not null, and not an empty string.
```php
// in this example the suffix field is required only when both the first_name and last_name are provided
$v->rule('requiredWith', 'suffix', ['first_name', 'last_name'], true);
Expand All @@ -306,7 +306,7 @@ $v->rules([
$v->validate();
```

Likewise, in this case `validate()` would still return true, as the suffix field would not be required in strict mode, as not all of the fields are provided.
Likewise, in this case `validate()` would still return true, as the suffix field would not be required in strict mode, as not all the fields are provided.
```php
$v = new Valitron\Validator(['first_name' => 'steve']);
$v->rules([
Expand All @@ -317,8 +317,31 @@ $v->rules([
$v->validate();
```

### Check Boolean Flag
The check boolean flag will enable the rule to work if the conditional field(s) are set too (bool)false. When not enabled, a (bool)false is treated as "SET" and would require the specified fields. Since this parameter comes after the strict flag, you must also set the strict flag to `true` or `false` when enabling the check boolean flag.
```php
// in this example the 'suffix' field is not required when the 'enabled' is included in the data and set to
// (bool)false. Without enabling the check boolean flag, this scenario would result in 'suffix' being required.
$v->rule('requiredWith', 'suffix', 'enabled', false, true);

// we can still use the strict flag as described when providing multiple fields; in this case,
// setting fields 'hidden' AND 'enabled' to (bool)true would make 'suffix' required but if either
// was set to (bool)false then 'suffix' would not be required when it previously would be.
$v->rule('requiredWith', 'suffix', ['enabled', 'hidden'], true, true);
```
Alternate syntax.
```php
$v = new Valitron\Validator(['first_name' => 'steve', 'last_name' => 'holt', 'suffix' => 'Mr']);
$v->rules([
'requiredWith' => [
['suffix', ['first_name', 'last_name'], true]
]
]);
$v->validate();
```

## requiredWithout fields usage
The `requiredWithout` rule checks that the field is required, not null, and not the empty string, if any other fields are NOT present.
The `requiredWithout` rule checks that the field is required, not null, and not an empty string, if any other fields are NOT present.
```php
// this rule will require the username field when the first_name is not present
$v->rule('requiredWithout', 'username', 'first_name')
Expand Down Expand Up @@ -355,7 +378,7 @@ $v->validate();
```

### Strict flag
The strict flag will change the `requiredWithout` rule to `requiredWithoutAll` which will require the field only if ALL of the other fields are not present.
The strict flag will change the `requiredWithout` rule to `requiredWithoutAll` which will require the field only if all the other fields are not present.
```php
// in this example the username field is required only when both the first_name and last_name are not provided
$v->rule('requiredWithout', 'username', ['first_name', 'last_name'], true);
Expand All @@ -371,7 +394,7 @@ $v->rules([
$v->validate();
```

Likewise, in this case `validate()` would still return true, as the username field would not be required in strict mode, as all of the fields are provided.
Likewise, in this case `validate()` would still return true, as the username field would not be required in strict mode, as all fields are provided.
```php
$v = new Valitron\Validator(['first_name' => 'steve', 'last_name' => 'holt']);
$v->rules([
Expand All @@ -382,6 +405,29 @@ $v->rules([
$v->validate();
```

### Check Boolean Flag
The check boolean flag will enable the rule to work if the conditional field is set to (bool)false. When not enabled, a (bool)false on the conditional field(s) are treated as "SET" and would require the specified fields. Since this parameter comes after the strict flag, you must also set the strict flag to `true` or `false` when enabling the check boolean flag.
```php
// in this example the 'mothers_name' field is required if the 'is_adult' field is included and set to (bool)false.
// Without enabling the check boolean flag, this scenario would result in mothers_name not being required.
$v->rule('requiredWithout', 'mothers_name', 'is_adult', false, true);

// we can still use the strict flag as described when providing multiple fields; in this case,
// setting fields 'is_adult' AND 'is_orphan' to (bool)false would make mothers_name required but if either
// was set to (bool)true then 'mothers_name' would not be required when it previously would be.
$v->rule('requiredWithout', 'mothers_name', ['is_adult', 'is_orphan'], true, true);
```
Alternate syntax.
```php
$v = new Valitron\Validator(['mothers_name' => 'Sharon Marsh', 'is_adult' => false, 'is_orphan' => false]);
$v->rules([
'requiredWithout' => [
['mothers_name', ['is_adult', 'is_orphan'], true, true]
]
]);
$v->validate();
```

## equals fields usage
The `equals` rule checks if two fields are equals in the data array, and that the second field is not null.
```php
Expand Down
32 changes: 23 additions & 9 deletions src/Valitron/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ protected function validateIn($field, $value, $params)
}

if ($forceAsAssociative || $this->isAssociativeArray($params[0])) {
$params[0] = array_keys($params[0]);
$params[0] = array_keys($params[0]);
}

$strict = false;
Expand Down Expand Up @@ -939,7 +939,7 @@ protected function validateInstanceOf($field, $value, $params)
}

/**
* Validates whether or not a field is required based on whether or not other fields are present.
* Validates whether a field is required based on whether other fields are present.
*
* @param string $field name of the field in the data array
* @param mixed $value value of this field
Expand All @@ -956,11 +956,19 @@ protected function validateRequiredWith($field, $value, $params, $fields)
$reqParams = is_array($params[0]) ? $params[0] : array($params[0]);
// check for the flag indicating if all fields are required
$allRequired = isset($params[1]) && (bool)$params[1];
// check for the flag indicating if boolean value on the required field should be accounted for
$checkBool = isset($params[2]) && (bool)$params[2];
$emptyFields = 0;
foreach ($reqParams as $requiredField) {
// check the field is set, not null, and not the empty string
if (isset($fields[$requiredField]) && !is_null($fields[$requiredField])
&& (is_string($fields[$requiredField]) ? trim($fields[$requiredField]) !== '' : true)) {
// check the field is set, not null, not boolean false (when flag enabled), and not an empty string
// if (isset($fields[$requiredField]) && !is_null($fields[$requiredField])
// && (is_string($fields[$requiredField]) ? trim($fields[$requiredField]) !== '' : true)) {
if (isset($fields[$requiredField]) && !is_null($fields[$requiredField]) &&
(
(is_string($fields[$requiredField]) ? trim($fields[$requiredField]) !== '' : true) &&
!($checkBool && is_bool($fields[$requiredField]) && $fields[$requiredField] === false) // Boolean false check when enabled
)) {

if (!$allRequired) {
$conditionallyReq = true;
break;
Expand All @@ -983,7 +991,7 @@ protected function validateRequiredWith($field, $value, $params, $fields)
}

/**
* Validates whether or not a field is required based on whether or not other fields are present.
* Validates whether a field is required based on whether other fields are present.
*
* @param string $field name of the field in the data array
* @param mixed $value value of this field
Expand All @@ -1000,11 +1008,17 @@ protected function validateRequiredWithout($field, $value, $params, $fields)
$reqParams = is_array($params[0]) ? $params[0] : array($params[0]);
// check for the flag indicating if all fields are required
$allEmpty = isset($params[1]) && (bool)$params[1];
// check for the flag indicating if boolean value on the required field should be accounted for
$checkBool = isset($params[2]) && (bool)$params[2];
$filledFields = 0;
foreach ($reqParams as $requiredField) {
// check the field is NOT set, null, or the empty string, in which case we are requiring this value be present
if (!isset($fields[$requiredField]) || (is_null($fields[$requiredField])
|| (is_string($fields[$requiredField]) && trim($fields[$requiredField]) === ''))) {
// check the field is NOT set, null, an empty string, or boolean false (when $checkBool flag is set),
// in which case we are requiring this value be present
if (!isset($fields[$requiredField]) || is_null($fields[$requiredField]) ||
(is_string($fields[$requiredField]) ? trim($fields[$requiredField]) === '' : true) ||
($checkBool && is_bool($fields[$requiredField]) && $fields[$requiredField] === false) // Boolean false check when enabled
) {

if (!$allEmpty) {
$conditionallyReq = true;
break;
Expand Down
2 changes: 1 addition & 1 deletion tests/Valitron/ValidateAddInstanceRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,6 @@ public function testUniqueRuleName()
$v->addInstanceRule("foo_rule", function () {
});
$u = $v->getUniqueRuleName("foo");
$this->assertRegExp("/^foo_rule_[0-9]{1,5}$/", $u);
$this->assertMatchesRegularExpression("/^foo_rule_[0-9]{1,5}$/", $u);
}
}
Loading