The database has a table with the id and Name fields. On the page I add a new value to the base through the form, which sends the values ​​to the model, and then to the base. Here is the form:

<?= $form->field($addform, 'nameEx')->textInput()->label('Exercise name')?> <div class="form-group"> <?= Html::submitButton('Отправить', ['class' => 'btn btn-primary']) ?> </div> 
Adding works. And how to organize the update values ​​in the database? For example, I added a value, but I want to replace Name, how to do it? Without using the generator Yii Began to do so:
 <?php foreach ($allNameEx as $item){ ?> <?= $form->field($addform, 'nameEx')->textInput(array('value'=>$item['name']))->label('');?> <a href="<?php echo Url::toRoute(['site/ex-delete', 'id' => $item['id']]) ?>" >Удалить</a> <?php } ?> <div class="form-group"> <?= Html::submitButton('Отправить', ['class' => 'btn btn-primary']) ?> </div> <?php ActiveForm::end(); ?> 

I add a form with input in which there is already a written Name value, but what to do next? After all, the form is one and takes the value nameEx one, respectively, when changing, everything will change to one value.

  • one
    Put the controller code and I'll explain everything - Urmuz Tagizade

1 answer 1

Add a hidden field containing the primary key of the table and the model will be updated according to it.

  • It works for one value in the form, it is great, and if I have many values ​​in the form, they need to specify different names (which should also be registered in the form model) how to automate? - Fitstd
  • one
    Then you can present the results of filling the form with an array. For this name input must be of the form name = "name [$ id]". In this case, $ _GET ['name'] will look like an array, where the array key will be the id of the element, and the value will be the text that the user entered. See the ActiveForm documentation to understand how to set the name of the input parameters. hiddenField in this case is not needed. - ilyaplot