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