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; }
/** * @param \yii\base\Model $model * @param string $attribute */ public function validateAttribute($model, $attribute) { $value = $model->{$attribute}; if (is_array($value) || is_object($value)) { $this->addError($model, $attribute, $this->message); return; } $model->{$attribute} = $this->transform($value); parent::validateAttribute($model, $attribute); if ($model->hasErrors($attribute)) { $model->{$attribute} = $value; } }
public function testEnsureCustomMessageIsSetOnValidateAttribute() { $val = new NumberValidator(['tooSmall' => '{attribute} is to small.', 'min' => 5]); $model = new FakedValidationModel(); $model->attr_number = 0; $val->validateAttribute($model, 'attr_number'); $this->assertTrue($model->hasErrors('attr_number')); $this->assertEquals(1, count($model->getErrors('attr_number'))); $msgs = $model->getErrors('attr_number'); $this->assertSame('attr_number is to small.', $msgs[0]); }
/** * @inheritdoc */ public function validateAttribute($model, $attribute) { $model->{$attribute} = str_replace(",", ".", $model->{$attribute}); parent::validateAttribute($model, $attribute); }