Duplicate fields with Yii2 Multipleinput

Asked

Viewed 39 times

0

I have a form that will need duplicity. It is an academic form.

In this form, it contains the following attributes, according to my model:

class Academico extends \app\models\MainModel
{
/**
 * {@inheritdoc}
 */
public static function tableName()
{
    return 'academico';
}

/**
 * {@inheritdoc}
 */
public function rules()
{
    return [
        [['member_id', 'instituicao', 'data_inicio', 'data_termino', 'cidade', 'estado', 'tipo'], 'required'],
        [['member_id', 'data_inicio', 'data_termino'], 'integer'],
        [['instituicao', 'cidade', 'estado', 'serie', 'curso', 'tipo'], 'string', 'max' => 191],
        [['member_id'], 'exist', 'skipOnError' => true, 'targetClass' => Usuario::className(), 'targetAttribute' => ['member_id' => 'id']],
    ];
}

/**
 * {@inheritdoc}
 */
public function attributeLabels()
{
    return [
        'id' => 'ID',
        'member_id' => 'Member ID',
        'instituicao' => 'Instituicao',
        'data_inicio' => 'Data Inicio',
        'data_termino' => 'Data Termino',
        'cidade' => 'Cidade',
        'estado' => 'Estado',
        'serie' => 'Serie',
        'curso' => 'Curso',
        'tipo' => 'Tipo',
    ];
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getMember()
{
    return $this->hasOne(Usuario::className(), ['id' => 'member_id']);
}

}

In my view, the attributes I upload are: 'institution', 'data_start', 'data_termino', 'city', 'state'.

I need to duplicate all these attributes without having to add one by one, as it is in the image:

inserir a descrição da imagem aqui

My view: `

<?php $form = ActiveForm::begin(
    ['id'=>'form-academico']
); ?>
<div class="row">
    <div class="col-md-3">
        <?= $form->field($model, 'instituicao')->widget(MultipleInput::className(['cloneButton' => true,])); ?>
    </div>
    <div class="col-md-3">
        <?= $form->field($model, 'data_inicio')->widget(MultipleInput::className()); ?>
    </div>
    <div class="col-md-3">
        <?= $form->field($model, 'data_fim')->widget(MultipleInput::className()); ?>
    </div
    <?php ActiveForm::end(); ?>

</div>

`

1 answer

1

You can do it in many ways, most of them involve Javascript.

With the use of Select2 (https://select2.org/) you can create a taginput, and in the backend separate the data sent by the user creating multiple records in database.

If each line should be a record, the ideal would be first to have only one (+) button, which would make the user interface less confusing, and with Javascript, to duplicate the line.

You will have to change the name of the fields (via javascript) if more than one line is added, to send an array of objects, for example:

If your $model::formName() is 'Lorem' The name of the first line would be Item[0][Lorem][institution], Item[0][Lorem][starting date], the second would be Item[1][Lorem][institution], Item[1][Lorem][starting date], in the backend to save you would make a

<?php
  foreach (\Yii::$app->request->post('Item') as $item) {
    $md = new Lorem();
    $md->import($item);
    $md->save();
  }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.