3

I want to make input field with tag using Input Tags Widget. But I got this error :

Either 'name', or 'model' and 'attribute' properties must be specified.

in /var/www/html/paramoor/vendor/yiisoft/yii2/widgets/InputWidget.php at line 75:

/**
 * Initializes the widget.
 * If you override this method, make sure you call the parent implementation first.
 */
public function init()
{
    if ($this->name === null && !$this->hasModel()) {
        throw new InvalidConfigException("Either 'name', or 'model' and 'attribute' properties must be specified.");
    }
    if (!isset($this->options['id'])) {
        $this->options['id'] = $this->hasModel() ? Html::getInputId($this->model, $this->attribute) : $this->getId();
    }
    parent::init();
}

And here's my View Code :

<?= $form->field($modelDetail, 'product_id')->widget(TagsinputWidget::classname(),
[
    'clientOptions' => [
        'trimValue' => true,
        'allowDuplicates' => false,
        'delimiter' => ';',
    ],
]) ?>
robsch
  • 9,358
  • 9
  • 63
  • 104
adn
  • 430
  • 2
  • 7
  • 20
  • What is `$modelDetail`? Is it a [Model](http://www.yiiframework.com/doc-2.0/yii-base-model.html) derived type? And are you sure with `product_id`? This attriubte contains the tags? Uncommon name ... – robsch Oct 30 '17 at 07:34

1 Answers1

0

When using widgets for fields/filters/etc., you need to provide one (or two) of those options. You have 2 options:

Give model and attribute:

<?= $form->field($modelDetail, 'product_id')->widget(TagsinputWidget::classname(),
[
    'model' => $modelDetail,
    'attribute' => 'product_id',
    'clientOptions' => [
        'trimValue' => true,
        'allowDuplicates' => false,
        'delimiter' => ';',
    ],
]) ?>

Give just name (as combined model and attribute name):

<?= $form->field($modelDetail, 'product_id')->widget(TagsinputWidget::classname(),
[
    'name' => 'ModelDetail[product_id]',
    'clientOptions' => [
        'trimValue' => true,
        'allowDuplicates' => false,
        'delimiter' => ';',
    ],
]) ?>

I suggest using first option as if model name is changed, you won't need to search where this model name was used as string.

Gynteniuxas
  • 7,035
  • 18
  • 38
  • 54
  • I've tried to add model or attribute or name like you do above, but it doesn't work. It still get the `Either 'name', or 'model' and 'attribute' properties must be specified.` error – adn Nov 01 '17 at 16:35