0

I want set default value in taggable for my update page but I can't do it!

This is my code in _form.php.

https://github.com/2amigos/yii2-taggable-behavior

 <?=
    $form->field($model, 'tags')->widget(SelectizeTextInput::className(), [
        // calls an action that returns a JSON object with matched
        // tags

        'loadUrl' => ['tags/list'],
        'options' => ['class' => 'form-control'],
        'clientOptions' => [
            'plugins' => ['remove_button'],
            'valueField' => 'name',
            'labelField' => 'name',
            'searchField' => ['name'],
            'create' => true,
        ],
    ])->hint('Use commas to separate tags')
    ?>

This is my model:

class Post extends \yii\db\ActiveRecord
{

    public $category;
    public $prime;
    public $metaNames;
    public $metaDec;
    public $tags;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'post';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
                [['menu', 'prime', 'slideshow', 'special', 'visit', 'deleted', 'active'], 'integer'],
                [['name', 'summary', 'text', 'sutitr', 'reference', 'slug', 'menu', 'slideshow', 'special', 'visit', 'created', 'modified', 'deleted', 'active'], 'required'],
                [['summary', 'tagDec', 'metaDec', 'tags', 'text', 'sutitr'], 'string'],
                [['created', 'modified'], 'safe'],
                ['category', 'each', 'rule' => ['integer']],
                [['tagNames'], 'safe'],
                [['headline'], 'string', 'max' => 255],
                [['name', 'reference'], 'string', 'max' => 100],
                [['slug'], 'string', 'max' => 200],
                [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
        ];
    }

I want set default tag in update page.

rob006
  • 21,383
  • 5
  • 53
  • 74
Saltern
  • 1,305
  • 2
  • 16
  • 42

1 Answers1

0

Tru use attribute value in SelectizeTextInput for set selected value:

SelectizeTextInput::widget([
    'name' => 'tags',
    'value' => 'love, this, game',
    'clientOptions' => [
        // ...
    ],
]);

And you need set the relationship to table post like in example to get your olready seted value.

public function getTags(){
    return $this->hasMany(Tag::className(), ['id' => 'tag_id'])->viaTable('post_tag_assn', ['post_id' => 'id']);
}
vityapro
  • 178
  • 1
  • 13
  • i want set static value ! i cant underastand that! – Saltern Aug 02 '17 at 09:29
  • then your code in form be some like that: `= $form->field($model, 'tags')->widget(SelectizeTextInput::className(), [ 'value'=>'some, static, value' 'loadUrl' => ['tags/list'], 'options' => ['class' => 'form-control'], 'clientOptions' => [ 'plugins' => ['remove_button'], 'valueField' => 'name', 'labelField' => 'name', 'searchField' => ['name'], 'create' => true, ], ])->hint('Use commas to separate tags') ?>` – vityapro Aug 02 '17 at 09:32
  • no! default selected value . automatic selected one tag when page is opening! – Saltern Aug 02 '17 at 10:31
  • 1
    Did you try to set defaul value in model if this is not new record? public $tags = !$this->isNewRecord ? 'some, static, value' : null; if you want some mor complicate conditions use yii2 [getters](http://www.yiiframework.com/doc-2.0/guide-concept-properties.html#properties) – vityapro Aug 02 '17 at 11:13