Convenience method to invalidate a field and translate the custom message.

@param Model $model
@param string $field
@param string $message
@param array $params
@return bool
489833
public function invalid(Model $model, $field, $message, $params = array()) {
        $model->invalidate($field, __d($model->validationDomain ?: 'default', $message, $params));

        return false;
    }
1174342
public static function findOrThrow($param, $field = '*', $parameters = array()) {
        $model = static::find($param, $field, $parameters);
        if (empty($model)) {
            throw new \InvalidArgumentException($param);
        }
        return $model;
    }
1058679
protected function _applyMessages(Model $model, array $rules) {
        foreach ($rules as $key => $value) {
            if (is_array($value)) {
                if ($key !== 'rule') {
                    $rules[$key] = $this->_applyMessages($model, $value);
                }

            // Collapsed rules
            } else if (isset($this->_messages[$value]) && $key !== 'rule') {
                $rules[$key] = array(
                    $value => array(
                        'rule' => $value,
                        'message' => $this->_messages[$value]
                    )
                );

            // Missing message
            } else if ($key === 'rule') {
                $rule = $value;

                if (is_array($rule)) {
                    $rule = array_shift($params);
                }

                if (isset($this->_messages[$rule]) && empty($rules['message'])) {
                    $rules['message'] = $this->_messages[$rule];
                }
            }
        }

        return $rules;
    }
1134407
protected function validateSubset($field, $value, $params)
    {
        if (!isset($params[0])) {
            return false;
        }
        if (!\is_array($params[0])) {
            $params[0] = [$params[0]];
        }
        if (\is_scalar($value)) {
            return $this->validateIn($field, $value, $params);
        }
        $intersect = \array_intersect($value, $params[0]);

        return \array_diff($value, $intersect) === \array_diff($intersect, $value);
    }
49245
public function validate($model, $fields = [])
    {
        $context = $this->_context;

        $context->messages = [];

        if (!$rules = $model->rules()) {
            return;
        }

        $context->model = $model;

        if (is_string($fields)) {
            $fields = (array)preg_split('#[\s,]+#', $fields, -1, PREG_SPLIT_NO_EMPTY);
        }

        $templates = null;
        foreach ($fields ?: $model->getChangedFields() as $field) {
            if (!isset($rules[$field])) {
                continue;
            }
            $context->field = $field;

            foreach ((array)$rules[$field] as $k => $v) {
                if (is_int($k)) {
                    $name = $v;
                    $parameters = null;
                } else {
                    $name = $k;
                    $parameters = $v;
                }

                $value = $this->_validate($model->$field, $name, $parameters);
                if ($value === null) {
                    $templates = $this->_templates ?: $this->_loadTemplates();
                    $template = isset($templates[$name]) ? $templates[$name] : $templates['default'];
                    $this->appendMessage(new Message($template, $context->model, $context->field, $parameters));
                } else {
                    $model->$field = $value;
                }
            }
        }

        if ($context->messages) {
            throw new ValidateFailedException('validate failed: ' . json_encode($context->messages));
        }
    }
920322
protected function validateDigitsBetween($field, $value, $params)
    {
        $length = strlen((string) $value);

        return !preg_match('/[^0-9]/', $value) && $length >= $params[0] && $length <= $params[1];
    }
618875
public static function invalidMessage($message)
    {
        $type = is_object($message) ? get_class($message) : gettype($message);

        return new static('The message should be an instance of '.Message::class.". Given `{$type}` is invalid.", 1);
    }
858304
public function t($message, $param = [], $name = 'app') {
        return Factory::i18n()->translate($message, $param, $name);
    }
17043
public function setError($param, $message, $translate = false, $translationParams = array())
    {
        $this->errors[] = new FormError($param, $message, $translate, $translationParams);

        // Call isValid to update valid status in restore data handlers
        $this->isValid();
    }
380162
protected function validate_equalsfield($field, $input, $param = null)
    {
        if (!isset($input[$field]) || empty($input[$field])) {
            return;
        }

        if ($input[$field] == $input[$param]) {
            return;
        }

        return array(
            'field' => $field,
            'value' => $input[$field],
            'rule' => __FUNCTION__,
            'param' => $param,
        );
    }
713243
public static function typeText($value, array $params, array $field)
    {
        if (!\is_string($value) && !is_numeric($value)) {
            return false;
        }

        $value = (string)$value;

        if (!empty($params['trim'])) {
            $value = trim($value);
        }

        if (isset($params['min']) && \strlen($value) < $params['min']) {
            return false;
        }

        if (isset($params['max']) && \strlen($value) > $params['max']) {
            return false;
        }

        $min = $params['min'] ?? 0;
        if (isset($params['step']) && (\strlen($value) - $min) % $params['step'] === 0) {
            return false;
        }

        if ((!isset($params['multiline']) || !$params['multiline']) && preg_match('/\R/um', $value)) {
            return false;
        }

        return true;
    }
85720
protected function validate_required($field, $input, $param = null)
    {
        if (isset($input[$field]) && ($input[$field] === false || $input[$field] === 0 || $input[$field] === 0.0 || $input[$field] === '0' || !empty($input[$field]))) {
            return;
        }

        return array(
            'field' => $field,
            'value' => null,
            'rule' => __FUNCTION__,
            'param' => $param,
        );
    }
1265807
private function callAfterFieldValidationMethod(Field $field)
    {
        $functionName = lcfirst($field->getName() . 'Validated');

        if (method_exists($this, $functionName)) {
            call_user_func([$this, $functionName]);
        }
    }
864260
protected function format($message, array $params) {
		if($fm = $this->formatParameters)
			$fm($params);

		foreach($params as $k=>$v) {
			if(is_string($v) || is_numeric($v))
				$message = str_replace(':'.$k, $v, $message);
		}
		return ucfirst($message);
	}
992364
public function formatMessage($message, $params)
    {
        $i18n = Yii::get('i18n', null, false);
        if (isset($i18n)) {
            return $i18n->format($message, $params);
        }

        return I18N::substitute($message, $params);
    }
274087
private function formatMessage($field, $msg, $params)
    {
        if (isset($this->attributes[$field])) {
            $msg = str_replace('{value}', $this->attributes[$field], $msg);
        }
        if (isset($this->_labels[$field])) {
            $msg = str_replace('{field}', $this->_labels[$field], $msg);
            if (is_array($params)) {
                $i = 1;
                foreach ($params as $k => $v) {
                    $tag = '{field' . $i . '}';
                    $label = isset($params[$k]) && (is_numeric($params[$k]) || is_string($params[$k])) && isset($this->_labels[$params[$k]]) ? $this->_labels[$params[$k]] : $tag;
                    $msg = str_replace($tag, $label, $msg);
                    $i++;
                }
            }
        } else {
            $msg = str_replace('{field}', $field, $msg);
        }
        return $msg;
    }
172287
public static <M extends PMessage<M,F>, F extends PField>
    MessageFieldArgument<M,F> toField(M message, F field) {
        return new MessageFieldArgument<>(message, field);
    }