Untitled

 avatar
unknown
plain_text
2 years ago
1.4 kB
4
Indexable
<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use wbraganca\dynamicform\DynamicFormWidget;

$form = ActiveForm::begin(['id' => 'dynamic-form']);

// render the fields for the main model
echo $form->field($model, 'name')->textInput(['maxlength' => true]);

// render the fields for the dynamic models
echo DynamicFormWidget::widget([
    'model' => $model,
    'attribute' => 'items',
    'min' => 1,
    'insertButton' => '.add-item',
    'deleteButton' => '.remove-item',
    'modelFields' => function($model, $index) {
        return [
            'name' => Html::textInput(
                'Item[' . $index . '][name]',
                ArrayHelper::getValue($model, 'name'),
                ['class' => 'form-control']
            ),
            'quantity' => Html::textInput(
                'Item[' . $index . '][quantity]',
                ArrayHelper::getValue($model, 'quantity'),
                ['class' => 'form-control']
            ),
            'price' => Html::textInput(
                'Item[' . $index . '][price]',
                ArrayHelper::getValue($model, 'price'),
                ['class' => 'form-control']
            ),
        ];
    },
    'options' => ['class' => 'form-inline', 'id' => 'items'],
]);

echo Html::submitButton('Submit', ['class' => 'btn btn-primary']);

ActiveForm::end();

?>
Editor is loading...