Validate attribute

@param \yii\base\Model $model
@param string $attribute

@return bool|void
12038
public function validateAttribute($model, $attribute)
    {
        $country = '';
        if ($this->country !== null) {
            $country = $this->country;
        } elseif ($this->countryAttribute !== null) {
            $country = $model->{$this->countryAttribute};
        } elseif ($this->countryCodeAttribute !== null) {
            $country = $model->{$this->countryCodeAttribute};
        }
        // if empty country and used strict mode
        if (empty($country) && $this->strict) {
            $this->addError($model, $attribute, Yii::t('app', $this->strictModeMessage));

            return false;
        }
        if (empty($country)) {
            return true;
        }
        $phoneUtil = PhoneNumberUtil::getInstance();
        try {
            $numberProto = $phoneUtil->parse($model->$attribute, $country);
            if ($phoneUtil->isValidNumber($numberProto)) {
                if ($this->format) {
                    $model->$attribute = $phoneUtil->format($numberProto, PhoneNumberFormat::INTERNATIONAL);
                }

                return true;
            } else {
                $this->addError($model, $attribute, Yii::t('app', $this->message));

                return false;
            }
        } catch (NumberParseException $e) {
            $this->addError($model, $attribute, Yii::t('app', $this->numberParseExceptionMessage));
        }
    }
1028283
protected function validateRequired($attribute, $value)
	{
		if (is_null($value))
		{
			return false;
		}
		elseif (is_string($value) && trim($value) === '')
		{
			return false;
		}
		elseif ($value instanceof File)
		{
			return (string) $value->getPath() != '';
		}

		return true;
	}
481044
public function validateAttribute($model, $attribute)
    {
        if (
            $this->targetClass !== null
            && $model->$attribute instanceof $this->targetClass
            && !empty($model->$attribute->isNewRecord)
        ) {
            return;
        }

        if ($model->isRelationPopulated($attribute)) {
            return;
        }

        parent::validateAttribute($model, $attribute);
    }
296836
protected function validate($attribute, $value)
    {
        $properties = array_merge($this->baseProperties, $this->properties);

        return in_array($attribute, $properties, true);
    }
914967
protected function validateBoolean( $attribute, $value )
	{
		if ( !$this->hasAttribute( $attribute ) )
		{
			return true;
		}

		$acceptable = [true, false, 0, 1, '0', '1'];

		return is_null( $value ) || in_array( $value, $acceptable, true );
	}
94715
public function validateAttribute($model, $attribute)
    {
        $value = $model->$attribute;
        if (!is_numeric($value) && !is_float($value)) {
            return $model->addError($attribute, Yii::t('luya', $this->message, ['attribute' => $model->getAttributeLabel($attribute)]));
        }
    }
379421
public function validatePassword($attribute)
    {
        if (!$this->hasErrors()) {
            $user = $this->getUser();
            
            if ($user && $this->userAttemptBruteForceLock($user)) {
                return $this->addError($attribute, Module::t('model_loginform_max_user_attempts', ['time' => Yii::$app->formatter->asRelativeTime($user->login_attempt_lock_expiration)]));
            }
            
            if (!$user || !$user->validatePassword($this->password)) {
                if ($this->attempts) {
                    // use `model_loginform_wrong_user_or_password` instead of `model_loginform_wrong_user_or_password_attempts` due to informations about correct email input.
                    $this->addError($attribute, Module::t('model_loginform_wrong_user_or_password', ['attempt' => $this->attempts, 'allowedAttempts' => $this->allowedAttempts]));
                } else {
                    $this->addError($attribute, Module::t('model_loginform_wrong_user_or_password'));
                }
            }
        }
    }
966085
protected function validateAttribute($object,$attribute)
	{
		$value=$object->$attribute;
		if($this->allowEmpty && $this->isEmpty($value))
			return;
		if($this->compareValue!==null)
			$compareTo=$compareValue=$this->compareValue;
		else
		{
			$compareAttribute=$this->compareAttribute===null ? $attribute.'_repeat' : $this->compareAttribute;
			$compareValue=$object->$compareAttribute;
			$compareTo=$object->getAttributeLabel($compareAttribute);
		}

		switch($this->operator)
		{
			case '=':
			case '==':
				if(($this->strict && $value!==$compareValue) || (!$this->strict && $value!=$compareValue))
					$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be repeated exactly.');
				break;
			case '!=':
				if(($this->strict && $value===$compareValue) || (!$this->strict && $value==$compareValue))
					$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must not be equal to "{compareValue}".');
				break;
			case '>':
				if($value<=$compareValue)
					$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be greater than "{compareValue}".');
				break;
			case '>=':
				if($value<$compareValue)
					$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be greater than or equal to "{compareValue}".');
				break;
			case '<':
				if($value>=$compareValue)
					$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be less than "{compareValue}".');
				break;
			case '<=':
				if($value>$compareValue)
					$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be less than or equal to "{compareValue}".');
				break;
			default:
				throw new CException(Yii::t('yii','Invalid operator "{operator}".',array('{operator}'=>$this->operator)));
		}
		if(!empty($message))
			$this->addError($object,$attribute,$message,array('{compareAttribute}'=>$compareTo,'{compareValue}'=>$compareValue));
	}
1256780
protected function validateTagAttribute(string $attribute) : bool
    {
        if (!empty($this->attributePattern)) {
            $matches = [];
            return preg_match($this->attributePattern, $attribute, $matches);
        } else {
            return empty($attribute);
        }
    }
646981
public function validateClass($attribute, $params)
    {
        $className = @\Yii::import($this->$attribute, true);

        if (!is_string($className) || !$this->classExists($className)) {
            $this->addError($attribute, "Class '$className' does not exist or has syntax error.");
        } elseif (isset($params['extends'])
            && ltrim($className, '\\') !== ltrim($params['extends'], '\\')
            && !is_subclass_of($className, $params['extends'])) {
            $this->addError('baseClass', "Class '$className' must extend from {$params['extends']}.");
        }
    }
944909
protected function validateAttribute($object, $attribute)
    {
        $value = $object->{$attribute};
        if ($this->isEmpty($value)) {
            $this->validateEmpty($object, $attribute);
            return;
        }
        if (!is_numeric($value)) {
            $message = $this->message !== null ? $this->message : '"{attribute}"必须是数字';
            $this->addError($object, $attribute, $message);
            return;
        }
        if ($this->integerOnly) {
            if (!preg_match($this->integerPattern, "$value")) {
                $message = $this->message !== null ? $this->message : '"{attribute}"必须是整数';
                $this->addError($object, $attribute, $message);
                return;
            }
        } else if (!preg_match($this->numberPattern, "$value")) {
            $message = null !== $this->message ? $this->message : '"{attribute}"必须是数字';
            $this->addError($object, $attribute, $message);
            return;
        }
        if (null !== $this->min && $value < $this->min) {
            $this->addError($object, $attribute, $this->tooSmallMessage, ['{min}' => $this->min]);
            return;
        }
        if (null !== $this->max && $value > $this->max) {
            $this->addError($object, $attribute, $this->tooBigMessage, ['{max}' => $this->max]);
        }
    }
220644
protected function validate( $attribute, $rule )
	{
		list( $rule, $parameters ) = $this->parseRule( $rule );

		if ( $rule == '' )
		{
			return;
		}

		// First we will get the correct keys for the given attribute in case the field is nested in
		// an array. Then we determine if the given rule accepts other field names as parameters.
		// If so, we will replace any asterisks found in the parameters with the correct keys.
		if ( ( $keys = $this->getExplicitKeys( $attribute ) ) && $this->dependsOnOtherFields( $rule ) )
		{
			$parameters = $this->replaceAsterisksInParameters( $parameters, $keys );
		}

		// We will get the value for the given attribute from the array of data and then
		// verify that the attribute is indeed validatable. Unless the rule implies
		// that the attribute is required, rules are not run for missing values.
		$value = $this->getValue( $attribute );

		$validatable = $this->isValidatable( $rule, $attribute, $value );

		$method = "validate{$rule}";

		if ( $validatable && !$this->$method( $attribute, $value, $parameters, $this ) )
		{
			$this->addFailure( $attribute, $rule, $parameters );
		}
	}
91550
public function validateReNewPassword($attribute, $params)
    {
        if (!$this->hasErrors()) {
            if ($this->new_password != $this->re_new_password) {
                $this->addError($attribute, Yii::t('app', 'Confirm new password error.'));
            }
        }
    }
1125217
public function validatePassword($attribute)
    {
        if (!$this->hasErrors()) {
            if ($this->user === null || !PasswordHelper::validate($this->password, $this->user->password_hash)) {
                $this->addError($attribute, Yii::t('yuncms', 'Invalid login or password'));
            }
        }
    }
1079454
protected function validateAttribute($object,$attribute)
	{
		if(!$this->setOnEmpty)
			$object->$attribute=$this->value;
		else
		{
			$value=$object->$attribute;
			if($value===null || $value==='')
				$object->$attribute=$this->value;
		}
	}
676628
private function assertValidAttribute($attribute)
    {
        if ( ! isset($this->documentChangeSet[$attribute])) {
            throw new \InvalidArgumentException(sprintf(
                'Attribute "%s" is not a valid attribute of the document "%s" in PreUpdateEventArgs.',
                $attribute,
                get_class($this->getDocument())
            ));
        }
    }
134441
protected function validate($attribute, $rule)
	{
		list($rule, $parameters) = $this->parseRule($rule);

		if ($rule == '') return;

		// We will get the value for the given attribute from the array of data and then
		// verify that the attribute is indeed validatable. Unless the rule implies
		// that the attribute is required, rules are not run for missing values.
		$value = $this->getValue($attribute);

		$validatable = $this->isValidatable($rule, $attribute, $value);

		$method = "validate{$rule}";

		if ($validatable && ! $this->$method($attribute, $value, $parameters, $this))
		{
			$this->addFailure($attribute, $rule, $parameters);
		}
	}