Closed ram0973 closed 10 years ago
Sorry, this is my fault. Just needed to write action like this:
public function actionUpdate($id) {
$model = $this->findModel($id);
$items = $model->getTags()->all();
$tags = [];
foreach ($items as $item)
{
$tags[] = $item->name;
};
$model->tagNames = implode(', ',$tags); **
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id, 'slug' => $model->slug]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
I am using your yii2-taggable extension with this widget, all working fine except one - when I editing my model, existing tags for this record is not showing, the tags field is empty. But autocomplete is working fine.
Here is my code:
form: <?= $form->field($model, 'tagNames')->widget(Selectize::className(), [ // calls an action that returns a JSON object with matched // tags 'url' => ['/news/tag/list'], 'options' => ['class' => 'form-control'], 'clientOptions' => [ 'plugins' => ['remove_button'], 'valueField' => 'name', 'labelField' => 'name', 'searchField' => ['name'], 'create' => true, ], ])->hint('Use commas to separate tags') ?>
tag model: public static function findAllByName($query) { return Tag::find()->where(['like', 'name', $query])->all(); }
tag controller: <?php
namespace app\modules\news\controllers; use yii\web\Response; use app\modules\news\models\Tag; use Yii;
class TagController extends \yii\web\Controller { public function actionList($query) { $models = Tag::findAllByName($query); $items = [];
}
Here is tags field is empty, but I certainly know, that it have a 2 tags: Autocomplete is ok: