Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.4 kB
1
Indexable
Never
<?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();

?>