yii\validators\StringValidator::validateAttribute PHP Method

validateAttribute() public method

public validateAttribute ( $model, $attribute )
    public function validateAttribute($model, $attribute)
    {
        $value = $model->{$attribute};
        if (!is_string($value)) {
            $this->addError($model, $attribute, $this->message);
            return;
        }
        $length = mb_strlen($value, $this->encoding);
        if ($this->min !== null && $length < $this->min) {
            $this->addError($model, $attribute, $this->tooShort, ['min' => $this->min]);
        }
        if ($this->max !== null && $length > $this->max) {
            $this->addError($model, $attribute, $this->tooLong, ['max' => $this->max]);
        }
        if ($this->length !== null && $length !== $this->length) {
            $this->addError($model, $attribute, $this->notEqual, ['length' => $this->length]);
        }
    }

Usage Example

 /**
  * @param $attribute
  */
 public function validatePassword($attribute)
 {
     if (empty($this->password)) {
         return;
     }
     $StringValidator = new StringValidator(['min' => 6]);
     $StringValidator->validateAttribute($this, 'password');
     $CompareValidator = new CompareValidator(['compareAttribute' => 'password']);
     $CompareValidator->validateAttribute($this, 'repassword');
 }
All Usage Examples Of yii\validators\StringValidator::validateAttribute