/** * Evaluates the value of the user. * The return result of this method will be assigned to the current attribute(s). * @param Event $event * @return mixed the value of the user. */ protected function getValue($event) { $attribute = $this->countryAttribute; $value = $this->owner->{$attribute}; if (is_numeric($value)) { return $value; } else { if (empty($value)) { return NULL; } else { $model = new RgnCountry(['name' => $value, 'status' => RgnCountry::STATUS_ACTIVE]); return $model->save(FALSE) ? $model->id : 0; } } }
/** * @inheritdoc */ public function rules() { return [['recordStatus', 'default', 'value' => static::RECORDSTATUS_USED], [['postcode', 'country_id'], 'required'], [['subdistrict_id', 'district_id', 'city_id', 'province_id'], 'safe'], [['recordStatus'], 'string'], [['postcode'], 'integer'], ['recordStatus', 'in', 'range' => [self::RECORDSTATUS_USED, self::RECORDSTATUS_DELETED]], ['country_id', 'exist', 'targetClass' => RgnCountry::className(), 'targetAttribute' => 'id', 'when' => function (RgnPostcode $model, $attribute) { $num = is_numeric($model->{$attribute}); //$model->addError($attribute, "num: [{$num}]; val: [{$model->$attribute}];"); return $num; }, 'message' => "Country doesn't exist."], ['province_id', 'exist', 'targetClass' => RgnProvince::className(), 'targetAttribute' => 'id', 'when' => function ($model, $attribute) { return is_numeric($model->{$attribute}); }, 'message' => "Province doesn't exist."], ['city_id', 'exist', 'targetClass' => RgnCity::className(), 'targetAttribute' => 'id', 'when' => function ($model, $attribute) { return is_numeric($model->{$attribute}); }, 'message' => "City doesn't exist."], ['district_id', 'exist', 'targetClass' => RgnDistrict::className(), 'targetAttribute' => 'id', 'when' => function ($model, $attribute) { return is_numeric($model->{$attribute}); }, 'message' => "District doesn't exist."], ['subdistrict_id', 'exist', 'targetClass' => RgnSubdistrict::className(), 'targetAttribute' => 'id', 'when' => function ($model, $attribute) { return is_numeric($model->{$attribute}); }, 'message' => "Subdistrict doesn't exist."]]; }
/** * @return \yii\db\ActiveQuery */ public function getCountry() { return $this->hasOne(\common\models\RgnCountry::className(), ['id' => 'country_id']); }
/** * @inheritdoc */ public function init() { parent::init(); $this->operation = new RgnCountryOperation(['model' => $this]); }
/** * @inheritdoc */ public function rules() { return [['recordStatus', 'default', 'value' => static::RECORDSTATUS_USED], [['name', 'country_id'], 'required'], [['recordStatus'], 'string'], [['number', 'abbreviation'], 'string', 'max' => 32], [['name'], 'string', 'max' => 255], [['created_at', 'updated_at', 'deleted_at', 'createdBy_id', 'updatedBy_id', 'deletedBy_id'], 'integer'], ['recordStatus', 'in', 'range' => [static::RECORDSTATUS_USED, static::RECORDSTATUS_DELETED]], ['country_id', 'exist', 'targetClass' => RgnCountry::className(), 'targetAttribute' => 'id', 'when' => function ($model, $attribute) { return is_numeric($model->{$attribute}); }, 'message' => "Country doesn't exist."]]; }