1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use Attribute; |
8
|
|
|
use Closure; |
9
|
|
|
use JetBrains\PhpStorm\ArrayShape; |
10
|
|
|
use Yiisoft\Validator\ParametrizedRuleInterface; |
11
|
|
|
use Yiisoft\Validator\BeforeValidationInterface; |
12
|
|
|
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait; |
13
|
|
|
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait; |
14
|
|
|
use Yiisoft\Validator\Rule\Trait\RuleNameTrait; |
15
|
|
|
use Yiisoft\Validator\ValidationContext; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Validates that the specified value is neither null nor empty. |
19
|
|
|
*/ |
20
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY)] |
21
|
|
|
final class Required implements ParametrizedRuleInterface, BeforeValidationInterface |
22
|
|
|
{ |
23
|
|
|
use BeforeValidationTrait; |
24
|
|
|
use HandlerClassNameTrait; |
25
|
|
|
use RuleNameTrait; |
26
|
|
|
|
27
|
14 |
|
public function __construct( |
28
|
|
|
private string $message = 'Value cannot be blank.', |
29
|
|
|
private bool $skipOnEmpty = false, |
30
|
|
|
private bool $skipOnError = false, |
31
|
|
|
/** |
32
|
|
|
* @var Closure(mixed, ValidationContext):bool|null |
33
|
|
|
*/ |
34
|
|
|
private ?Closure $when = null, |
35
|
|
|
) { |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
6 |
|
public function getMessage(): string |
42
|
|
|
{ |
43
|
6 |
|
return $this->message; |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
#[ArrayShape(['message' => 'string[]', 'skipOnEmpty' => 'bool', 'skipOnError' => 'bool'])] |
47
|
|
|
public function getOptions(): array |
48
|
|
|
{ |
49
|
|
|
return [ |
50
|
|
|
'message' => [ |
51
|
2 |
|
'message' => $this->message, |
52
|
|
|
], |
53
|
2 |
|
'skipOnEmpty' => $this->skipOnEmpty, |
54
|
2 |
|
'skipOnError' => $this->skipOnError, |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|