Answer the question
In order to leave comments, you need to log in
Is it possible to merge attributeLabels of model and behavior in Yii2?
Good morning,
I figured out the rules - how it's done, something like this:
public function attach($owner)
{
parent::attach($owner);
$owner->on(ActiveRecord::EVENT_BEFORE_INSERT,[$this,'onBeforeSave']);
$owner->on(ActiveRecord::EVENT_BEFORE_UPDATE,[$this,'onBeforeSave']);
$owner->on(ActiveRecord::EVENT_AFTER_INSERT,[$this,'onAfterSave']);
$owner->on(ActiveRecord::EVENT_AFTER_UPDATE,[$this,'onAfterSave']);
$owner->on(ActiveRecord::EVENT_AFTER_DELETE,[$this,'onAfterDelete']);
$validators = $owner->getValidators();
$validators[] = Validator::createValidator('integer', $this->owner, 'date_create_stamp');
$validators[] = Validator::createValidator('integer', $this->owner, 'date_update_stamp');
$validators[] = Validator::createValidator('integer', $this->owner, 'author_id');
$validators[] = Validator::createValidator('integer', $this->owner, 'exist');
$validators[] = Validator::createValidator('integer', $this->owner, 'sort');
$validators[] = Validator::createValidator('default', $this->owner, ['date_create','date_update'], ['date', 'value' => date('Y-m-d H:i:s')]);
$validators[] = Validator::createValidator('date', $this->owner, ['date_create','date_update'], ['format' => date('Y-m-d H:i:s')]);
$owner->validators = $validators;
}
Answer the question
In order to leave comments, you need to log in
To tell the truth I did not understand why such a bunch of tags. In my opinion, it is easier to attach the rules like this:
public function attach($owner)
{
parent::attach($owner);
$validator = \yii\validators\Validator::createValidator('safe', $owner, 'new_tags');
$owner->validators[] = $validator;
}
$owner->setAttribute('some_attribute', 'some_attribute')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question