yii\validators\NumberValidator::validateAttribute PHP Method

validateAttribute() public method

public validateAttribute ( $model, $attribute )
    public function validateAttribute($model, $attribute)
    {
        $value = $model->{$attribute};
        if (is_array($value) || is_object($value) && !method_exists($value, '__toString')) {
            $this->addError($model, $attribute, $this->message);
            return;
        }
        $pattern = $this->integerOnly ? $this->integerPattern : $this->numberPattern;
        if (!preg_match($pattern, "{$value}")) {
            $this->addError($model, $attribute, $this->message);
        }
        if ($this->min !== null && $value < $this->min) {
            $this->addError($model, $attribute, $this->tooSmall, ['min' => $this->min]);
        }
        if ($this->max !== null && $value > $this->max) {
            $this->addError($model, $attribute, $this->tooBig, ['max' => $this->max]);
        }
    }

Usage Example

Beispiel #1
0
 public function validateAttribute($model, $attribute)
 {
     $result = parent::validateAttribute($model, $attribute);
     /* Not sure about this, left it here in case
     		if(
     			count($model->getErrors($attribute)) <= 0 && 
     			$this->format === null && 
     			$this->format !== false
     		){
     			
     			$value = $model->$attribute;
     			
     			if(preg_match('#^0#', $value)){
     				// Starts with 0, string it
     				$model->$attribute = $value;
     			}elseif(preg_match('#([0-9]+)\.([0-9]+)#')){
     				$model->$attribute = floatval($value);
     			}else{
     				$model->$attribute = $value;
     			}
     			
     		}else{
     			$model->$attribute = $this->format($model->$attribute);
     		}
     		*/
     $model->{$attribute} = $this->format($model->{$attribute});
     return $result;
 }
All Usage Examples Of yii\validators\NumberValidator::validateAttribute