Webiny\Component\Entity\Attribute\AbstractAttribute::applyValidator PHP Method

applyValidator() protected method

Apply validator to given value
protected applyValidator ( string $validator, string $key, mixed $value, array $messages = [] )
$validator string
$key string
$value mixed
$messages array
    protected function applyValidator($validator, $key, $value, $messages = [])
    {
        try {
            if ($this->isString($validator)) {
                $params = $this->arr(explode(':', $validator));
                $vName = '';
                $validatorParams = [$this, $value, $params->removeFirst($vName)->val()];
                $validator = Entity::getInstance()->getValidator($vName);
                if (!$validator) {
                    throw new ValidationException('Validator does not exist');
                }
                $validator->validate(...$validatorParams);
            } elseif ($this->isCallable($validator)) {
                $vName = 'callable';
                $validator($value, $this);
            }
        } catch (ValidationException $e) {
            $msg = isset($messages[$vName]) ? $messages[$vName] : $e->getMessage();
            $ex = new ValidationException($msg);
            $ex->addError($key, $msg);
            throw $ex;
        }
    }