Author: Eugene Tupikov (unclead.nsk@gmail.com)
Inheritance: extends yii\widgets\InputWidget
 /**
  * @inheritdoc
  */
 protected function renderWidget($type, $name, $value, $options)
 {
     // Extend options in case of rendering embedded MultipleInput
     // We have to pass to the widget an original model and an attribute to be able get a first error from model
     // for embedded widget.
     if ($type === MultipleInput::className()) {
         $model = $this->context->model;
         // in case of embedding level 2 and more
         if (preg_match('/^([\\w\\.]+)(\\[.*)$/', $this->context->attribute, $matches)) {
             $search = sprintf('%s[%s]%s', $model->formName(), $matches[1], $matches[2]);
         } else {
             $search = sprintf('%s[%s]', $model->formName(), $this->context->attribute);
         }
         $replace = $this->context->attribute;
         $attribute = str_replace($search, $replace, $name);
         $options['model'] = $model;
         $options['attribute'] = $attribute;
         // Remember current name and mark the widget as embedded to prevent
         // generation of wrong prefix in case when column is associated with AR relation
         // @see https://github.com/unclead/yii2-multiple-input/issues/92
         $options['name'] = $name;
         $options['isEmbedded'] = true;
     }
     return parent::renderWidget($type, $name, $value, $options);
 }
use yii\bootstrap\ActiveForm;
use unclead\multipleinput\MultipleInput;
use unclead\multipleinput\examples\models\ExampleModel;
use yii\helpers\Html;
use unclead\multipleinput\MultipleInputColumn;
/* @var $this \yii\web\View */
/* @var $model ExampleModel */
$commonAttributeOptions = ['enableAjaxValidation' => false, 'enableClientValidation' => false, 'validateOnChange' => false, 'validateOnSubmit' => true, 'validateOnBlur' => false];
$enableActiveForm = false;
?>

<?php 
if ($enableActiveForm) {
    $form = ActiveForm::begin(['enableAjaxValidation' => true, 'enableClientValidation' => false, 'validateOnChange' => false, 'validateOnSubmit' => true, 'validateOnBlur' => false]);
} else {
    echo Html::beginForm();
}
?>

<?php 
echo MultipleInput::widget(['model' => $model, 'attribute' => 'questions', 'attributeOptions' => $commonAttributeOptions, 'columns' => [['name' => 'question', 'type' => 'textarea'], ['name' => 'answers', 'type' => MultipleInput::className(), 'options' => ['attributeOptions' => $commonAttributeOptions, 'columns' => [['name' => 'right', 'type' => MultipleInputColumn::TYPE_CHECKBOX], ['name' => 'answer']]]]]]);
?>

<?php 
echo Html::submitButton('Update', ['class' => 'btn btn-success']);
if ($enableActiveForm) {
    ActiveForm::end();
} else {
    echo Html::endForm();
}
 /**
  * Register script.
  *
  * @throws \yii\base\InvalidParamException
  */
 protected function registerAssets()
 {
     $view = $this->context->getView();
     MultipleInputAsset::register($view);
     $view = $this->context->getView();
     $jsBefore = [];
     if (is_array($view->js) && array_key_exists(View::POS_READY, $view->js)) {
         foreach ($view->js[View::POS_READY] as $key => $js) {
             $jsBefore[$key] = $js;
         }
     }
     $template = $this->prepareTemplate();
     $jsTemplates = [];
     if (is_array($view->js) && array_key_exists(View::POS_READY, $view->js)) {
         foreach ($view->js[View::POS_READY] as $key => $js) {
             if (array_key_exists($key, $jsBefore)) {
                 continue;
             }
             $jsTemplates[$key] = $js;
             unset($view->js[View::POS_READY][$key]);
         }
     }
     $options = Json::encode(['id' => $this->id, 'template' => $template, 'jsTemplates' => $jsTemplates, 'max' => $this->max, 'min' => $this->min, 'attributes' => $this->prepareJsAttributes(), 'indexPlaceholder' => $this->getIndexPlaceholder()]);
     $js = "jQuery('#{$this->id}').multipleInput({$options});";
     $view->registerJs($js);
 }
Esempio n. 4
0
?>
" style="display: none">
                <?php 
echo $this->render('_blocks', ['model' => $model]);
?>
            </div>

        </div>

        <div class="col-md-12">
            <h4><?php 
echo Yii::t('cms', 'Meta Tags');
?>
</h4>
            <?php 
echo $form->field($model, 'meta')->widget(MultipleInput::className(), ['columns' => [['name' => 'name', 'title' => Yii::t('cms', 'Name'), 'type' => 'dropDownList', 'items' => call_user_func([Page::getDefinitionClass(), 'getMetaTypesList'])], ['name' => 'content', 'title' => Yii::t('cms', 'Content')]]])->label(false);
?>
        </div>
    </div>


    <div class="form-group">
        <?php 
echo Html::submitButton($model->isNewRecord ? Yii::t('cms', 'Create') : Yii::t('cms', 'Save'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']);
?>
    </div>

    <?php 
ActiveForm::end();
?>
/* @var $this \yii\web\View */
/* @var $model ExampleModel */
?>

<?php 
$form = ActiveForm::begin(['enableAjaxValidation' => true, 'enableClientValidation' => false, 'validateOnChange' => false, 'validateOnSubmit' => true, 'validateOnBlur' => false]);
?>

<h3>Single column</h3>
<?php 
echo $form->field($model, 'emails')->widget(MultipleInput::className(), ['max' => 6, 'allowEmptyList' => false, 'columns' => [['name' => 'emails', 'options' => ['placeholder' => 'E-mail']]], 'min' => 2, 'addButtonPosition' => [MultipleInput::POS_HEADER, MultipleInput::POS_FOOTER, MultipleInput::POS_ROW]])->label(false);
?>

<h3>Multiple columns</h3>
<?php 
echo $form->field($model, 'schedule')->widget(MultipleInput::className(), ['max' => 4, 'allowEmptyList' => true, 'rowOptions' => function ($model) {
    $options = [];
    if ($model['priority'] > 1) {
        $options['class'] = 'danger';
    }
    return $options;
}, 'columns' => [['name' => 'user_id', 'type' => MultipleInputColumn::TYPE_DROPDOWN, 'enableError' => true, 'title' => 'User', 'defaultValue' => 33, 'items' => [31 => 'item 31', 32 => 'item 32', 33 => 'item 33', 34 => 'item 34', 35 => 'item 35', 36 => 'item 36']], ['name' => 'day', 'type' => DatePicker::className(), 'title' => 'Day', 'value' => function ($data) {
    return $data['day'];
}, 'items' => ['0' => 'Saturday', '1' => 'Monday'], 'options' => ['pluginOptions' => ['format' => 'dd.mm.yyyy', 'todayHighlight' => true]], 'headerOptions' => ['style' => 'width: 250px;', 'class' => 'day-css-class']], ['name' => 'priority', 'title' => 'Priority', 'defaultValue' => 1, 'enableError' => true, 'options' => ['class' => 'input-priority']], ['name' => 'comment', 'type' => MultipleInputColumn::TYPE_STATIC, 'value' => function ($data) {
    return Html::tag('span', 'static content', ['class' => 'label label-info']);
}, 'headerOptions' => ['style' => 'width: 70px;']], ['type' => MultipleInputColumn::TYPE_CHECKBOX_LIST, 'name' => 'enable', 'headerOptions' => ['style' => 'width: 80px;'], 'items' => [1 => 'Test 1', 2 => 'Test 2', 3 => 'Test 3', 4 => 'Test 4'], 'options' => ['unselect' => 2]]]]);
?>

<?php 
echo Html::submitButton('Update', ['class' => 'btn btn-success']);
ActiveForm::end();