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



我想为我的更新页面设置可标记的默认值,但我做不到!

这是我在_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 yiidbActiveRecord
{
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']],
];
}

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

Tru 在 SelectizeTextInput 中使用属性value来设置选定的值:

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

您需要像示例中一样将关系设置为表格帖子,以获得您的 olready 设置值。

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

最新更新