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\Formatter;
use Yiisoft\Validator\FormatterInterface;
use Yiisoft\Validator\Result;
use Yiisoft\Validator\Rule\Trait\EmptyCheckTrait;
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;
private FormatterInterface $formatter;
public function __construct(?FormatterInterface $formatter = null)
$this->formatter = $formatter ?? new Formatter();
}
public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result
if (!$rule instanceof Required) {
throw new UnexpectedRuleException(Required::class, $rule);
$result = new Result();
if ($this->isEmpty(is_string($value) ? trim($value) : $value)) {
$formattedMessage = $this->formatter->format(
$rule->getMessage(),
['attribute' => $context?->getAttribute(), 'value' => $value]
);
$result->addError($formattedMessage);
return $result;