-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathRequiredWithout.php
More file actions
32 lines (24 loc) · 975 Bytes
/
RequiredWithout.php
File metadata and controls
32 lines (24 loc) · 975 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
declare(strict_types=1);
namespace Sirius\Validation\Rule;
class RequiredWithout extends Required
{
const OPTION_ITEM = 'item';
const MESSAGE = 'This field is required';
const LABELED_MESSAGE = '{label} is required';
protected array $optionsIndexMap = [
0 => self::OPTION_ITEM
];
public function validate(mixed $value, ?string $valueIdentifier = null): bool
{
$this->value = $value;
$relatedItemPath = $this->getRelatedValueIdentifier((string) $valueIdentifier, $this->options[self::OPTION_ITEM]);
$relatedItemValue = $this->context && $relatedItemPath !== null ? $this->context->getItemValue($relatedItemPath) : null;
if (isset($this->options[self::OPTION_ITEM]) && $relatedItemValue === null) {
$this->success = ($value !== null && (!is_string($value) || trim($value) !== ''));
} else {
$this->success = true;
}
return $this->success;
}
}