yii2-json-behavior icon indicating copy to clipboard operation
yii2-json-behavior copied to clipboard

Default value for initialization

Open Horat1us opened this issue 7 years ago • 2 comments

Shouldn't it be property to define default value for JsonField? For example

<?php
class Model extends ActiveRecord {
    public function behaviors() {
        return ['json' => [
            'class' => JsonBehavior::class,
            'attributes' => 'settings',
            'default' => ['key' => 'value],
        ];
    }
}

I need it because I may use settings before validation, so I cant use default validator and need to create own behavior that do initialization. If you want I can create PR.

Horat1us avatar Dec 12 '17 18:12 Horat1us

Why do not you use the value when initializing?

class Model extends ActiveRecord {
    public function behaviors() {
        return ['json' => [
            'class' => JsonBehavior::class,
            'attributes' => 'settings',
        ];
    }
    
    public function init()
    {
        $this->settings = new JsonField(['key' => 'value']);
    }
}

paulzi avatar Mar 21 '18 20:03 paulzi

Default in configuration is more friendly. Anyway, after 2.0.14 yii release I do not use this library because of built in json support

Horat1us avatar Mar 22 '18 08:03 Horat1us