for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace Yiisoft\Validator\Rule;
use Yiisoft\Validator\Exception\UnexpectedRuleException;
use Yiisoft\Validator\Result;
use Yiisoft\Validator\Rule\Trait\EmptyCheckTrait;
use Yiisoft\Validator\RuleHandlerInterface;
use Yiisoft\Validator\ValidationContext;
use function is_string;
/**
* Validates that the specified value is neither null nor empty.
*/
final class RequiredHandler implements RuleHandlerInterface
{
use EmptyCheckTrait;
public function validate(mixed $value, object $rule, ValidationContext $context): Result
if (!$rule instanceof Required) {
throw new UnexpectedRuleException(Required::class, $rule);
}
$result = new Result();
if ($this->isEmpty(is_string($value) ? trim($value) : $value)) {
$result->addError(
message: $rule->getMessage(),
parameters: ['value' => $value]
);
return $result;