如何在2 amigos/yii2-taggable-behavior中设置默认值

时间:2017-08-02 07:06:51

标签: yii2 yii2-extension

我想在我的更新页面的taggable中设置默认值,但我不能这样做!

这是_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')
    ?>

这是我的模特:

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']],
        ];
    }

我想在更新页面中设置默认标记。

1 个答案:

答案 0 :(得分:0)

SelectizeTextInput中使用属性value来设置所选值:

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

您需要将关系设置为与example中的表格相关联,以获得您已经设定的价值。

public function getTags(){
    return $this->hasMany(Tag::className(), ['id' => 'tag_id'])->viaTable('post_tag_assn', ['post_id' => 'id']);
}