Answer the question
In order to leave comments, you need to log in
How to add many values at once?
Another question on Yii2, I used the extension https://github.com/unclead/yii2-multiple-input , I installed it like I did everything as it is written in the documentation, but the problem is that the data in the database is not stored in an array, but by one record, tell me, where did I go wrong again? Thank you!
my Controller:
public function actionCreate()
{
$models = [new Item()];
$request = Yii::$app->getRequest();
if ($request->isPost && $request->post('ajax') !== null) {
$data = Yii::$app->request->post('Item', []);
foreach (array_keys($data) as $index) {
$models[$index] = new Item();
}
Model::loadMultiple($models, Yii::$app->request->post());
Yii::$app->response->format = Response::FORMAT_JSON;
$result = ActiveForm::validateMultiple($models);
return $result;
}
if (Model::loadMultiple($models, Yii::$app->request->post())) {
foreach ($models as $model) {
$model->save(false);
}
return $this->redirect('index');
}
return $this->render('create', [
'models' => $models,
]);
}
<?= TabularInput::widget([
'models' => $models,
'modelClass' => Item::class,
'cloneButton' => true,
'sortable' => false,
'min' => 1,
'max' => 5,
'addButtonPosition' => [
TabularInput::POS_HEADER,
],
'layoutConfig' => [
'offsetClass' => 'col-sm-offset-4',
'labelClass' => 'col-sm-2',
'wrapperClass' => 'col-sm-10',
'errorClass' => 'col-sm-4'
],
'attributeOptions' => [
'enableAjaxValidation' => true,
'enableClientValidation' => false,
'validateOnChange' => false,
'validateOnSubmit' => true,
'validateOnBlur' => false,
],
'form' => $form,
'columns' => [
[
'name' => 'id',
'type' => TabularColumn::TYPE_HIDDEN_INPUT
],
[
'name' => 'title',
'title' => 'Title',
'type' => TabularColumn::TYPE_TEXT_INPUT,
'attributeOptions' => [
'enableClientValidation' => true,
'validateOnChange' => true,
],
'enableError' => true
],
[
'name' => 'description',
'title' => 'Description',
],
],
]) ?>
<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "item".
*
* @property int $id
* @property string $title
* @property string $description
* @property string $file
* @property string $date
*/
class Item extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'item';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['title', 'description', 'file', 'date'], 'required'],
[['title', 'description', 'file', 'date'], 'string', 'max' => 255],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'title' => 'Title',
'description' => 'Description',
'file' => 'File',
'date' => 'Date',
];
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question