E
E
EVOSandru62017-09-16 05:26:22
Yii
EVOSandru6, 2017-09-16 05:26:22

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;
    }

And here with attributeLabels did not find the information. Tell me please!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-09-16
@EVOSandru6

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;
  }

label did not try, but I think it should work like this:
$owner->setAttribute('some_attribute', 'some_attribute')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question