具有基于变量值的记录数的 Yii2 动态形式
Yii2 dynamic form with number of record based on variable value
我无法保存所有记录,只能保存最后一条。
在表单中用户插入 Year_begin
和 Year_end
变量$years
(其中$years = Year_end - Year_begin
)被传递给动态表单。
示例:
- 2010 年和 2011 年工作 我需要生成 两个 条记录
- 从 2010 年工作到 2015 年我需要生成 6 条记录
我不想使用“+”和“-”按钮生成新记录。
每条记录都显示正确的 YEAR 值
这是结果:
这是我的 while 循环表单代码:
<?php foreach ($modelsComm as $i => $modelComm): ?>
<div class="item panel panel-default"><!-- widgetBody -->
<div class="panel-heading">
<h3 class="panel-title pull-left">id_calcolo</h3>
<div class="pull-right">
<button type="button" class="add-item btn btn-success btn-xs"><i class="glyphicon glyphicon-plus"></i></button>
<button type="button" class="remove-item btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button>
</div>
<div class="clearfix"></div>
</div>
<div class="panel-body">
<!-- loop begin -->
<?php
$n=0;
while ($n < $years) {
// necessary for update action.
if (! $modelComm->isNewRecord) {
echo Html::activeHiddenInput($modelComm, "[{$i}]id");
}
?>
<div class="row">
<div class="col-sm-2">
<?= $form->field($modelComm, "[{$i}]year")->textInput(['value' => $year_begin+$n]) ?>
</div>
<div class="col-sm-4">
<?= $form->field($modelComm, "[{$i}]worked")->textInput(['maxlength' => true]) ?>
</div>
<div class="col-sm-4">
<?= $form->field($modelComm, "[{$i}]paid")->textInput(['maxlength' => true]) ?>
</div>
</div><!-- .row -->
<?php
$n++;
}
?>
<!-- loop end -->
</div>
</div>
<?php endforeach; ?>
我的控制器功能:
public function actionCreate($years,$year_begin)
{
$model = new CalcReference();
$modelsComm = [new Comm];
if ($model->load(Yii::$app->request->post()) && $model->save())
{
$modelsComm = Model::createMultiple(Comm::classname());
Model::loadMultiple($modelsComm, Yii::$app->request->post());
// ajax validation
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ArrayHelper::merge(
ActiveForm::validateMultiple($modelsComm),
ActiveForm::validate($model)
);
}
// validate all models
$valid = $model->validate();
$valid = Model::validateMultiple($modelsComm) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
foreach ($modelsComm as $modelComm) {
$modelComm->id_pratica = $model->id;
if (! ($flag = $modelComm->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
return $this->redirect(['view',
'id' => $model->id,
'year_begin' => $yeear_begin,
'years' => $years,
]);
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
/* return $this->redirect(['view', 'id' => $model->id,
'anno_inizio' => $anno_inizio,
'qta_anni' => $qta_anni,
'dal' => $dal,
'al' => $al,
'id_pratica' => $id_pratica,
]);*/
} else
{
return $this->render('create', [
'model' => $model,
'modelsComm' => (empty($modelsComm)) ? [new Comm] : $modelsComm,
'year_begin' => $yeear_begin,
'years' => $years,
]);
}
}
我的模特:
public static function tableName()
{
return 'comm';
}
public function rules()
{
return [
[['year', 'worked', ], 'required'],
[['worked', 'paid'], 'integer'],
];
}
public function attributeLabels()
{
return [
'id' => 'ID',
'year' => 'Year',
'worked' => 'Worked days',
'paid' => 'Total Paid',
];
}
这是型号 Model.php:
class Model extends \yii\base\Model
{
/**
* Creates and populates a set of models.
*
* @param string $modelClass
* @param array $multipleModels
* @return array
*/
public static function createMultiple($modelClass, $multipleModels = [])
{
$model = new $modelClass;
$formName = $model->formName();
$post = Yii::$app->request->post($formName);
$models = [];
if (! empty($multipleModels)) {
$keys = array_keys(ArrayHelper::map($multipleModels, 'id', 'id'));
$multipleModels = array_combine($keys, $multipleModels);
}
if ($post && is_array($post)) {
foreach ($post as $i => $item) {
if (isset($item['id']) && !empty($item['id']) && isset($multipleModels[$item['id']])) {
$models[] = $multipleModels[$item['id']];
} else {
$models[] = new $modelClass;
}
}
}
unset($model, $formName, $post);
return $models;
在年循环中你使用 $n
但输入名称里面有 $i
。所以你所有的输入都用 [0]
前缀初始化。这就是为什么在服务器端你只有最后一条记录。
只需将[{$i}]id
更改为[{$n}]id
,将[{$i}]year
更改为[{$n}]year
等等
我无法保存所有记录,只能保存最后一条。
在表单中用户插入 Year_begin
和 Year_end
变量$years
(其中$years = Year_end - Year_begin
)被传递给动态表单。
示例:
- 2010 年和 2011 年工作 我需要生成 两个 条记录
- 从 2010 年工作到 2015 年我需要生成 6 条记录
我不想使用“+”和“-”按钮生成新记录。
每条记录都显示正确的 YEAR 值
这是结果:
这是我的 while 循环表单代码:
<?php foreach ($modelsComm as $i => $modelComm): ?>
<div class="item panel panel-default"><!-- widgetBody -->
<div class="panel-heading">
<h3 class="panel-title pull-left">id_calcolo</h3>
<div class="pull-right">
<button type="button" class="add-item btn btn-success btn-xs"><i class="glyphicon glyphicon-plus"></i></button>
<button type="button" class="remove-item btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button>
</div>
<div class="clearfix"></div>
</div>
<div class="panel-body">
<!-- loop begin -->
<?php
$n=0;
while ($n < $years) {
// necessary for update action.
if (! $modelComm->isNewRecord) {
echo Html::activeHiddenInput($modelComm, "[{$i}]id");
}
?>
<div class="row">
<div class="col-sm-2">
<?= $form->field($modelComm, "[{$i}]year")->textInput(['value' => $year_begin+$n]) ?>
</div>
<div class="col-sm-4">
<?= $form->field($modelComm, "[{$i}]worked")->textInput(['maxlength' => true]) ?>
</div>
<div class="col-sm-4">
<?= $form->field($modelComm, "[{$i}]paid")->textInput(['maxlength' => true]) ?>
</div>
</div><!-- .row -->
<?php
$n++;
}
?>
<!-- loop end -->
</div>
</div>
<?php endforeach; ?>
我的控制器功能:
public function actionCreate($years,$year_begin)
{
$model = new CalcReference();
$modelsComm = [new Comm];
if ($model->load(Yii::$app->request->post()) && $model->save())
{
$modelsComm = Model::createMultiple(Comm::classname());
Model::loadMultiple($modelsComm, Yii::$app->request->post());
// ajax validation
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ArrayHelper::merge(
ActiveForm::validateMultiple($modelsComm),
ActiveForm::validate($model)
);
}
// validate all models
$valid = $model->validate();
$valid = Model::validateMultiple($modelsComm) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
foreach ($modelsComm as $modelComm) {
$modelComm->id_pratica = $model->id;
if (! ($flag = $modelComm->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
return $this->redirect(['view',
'id' => $model->id,
'year_begin' => $yeear_begin,
'years' => $years,
]);
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
/* return $this->redirect(['view', 'id' => $model->id,
'anno_inizio' => $anno_inizio,
'qta_anni' => $qta_anni,
'dal' => $dal,
'al' => $al,
'id_pratica' => $id_pratica,
]);*/
} else
{
return $this->render('create', [
'model' => $model,
'modelsComm' => (empty($modelsComm)) ? [new Comm] : $modelsComm,
'year_begin' => $yeear_begin,
'years' => $years,
]);
}
}
我的模特:
public static function tableName()
{
return 'comm';
}
public function rules()
{
return [
[['year', 'worked', ], 'required'],
[['worked', 'paid'], 'integer'],
];
}
public function attributeLabels()
{
return [
'id' => 'ID',
'year' => 'Year',
'worked' => 'Worked days',
'paid' => 'Total Paid',
];
}
这是型号 Model.php:
class Model extends \yii\base\Model
{
/**
* Creates and populates a set of models.
*
* @param string $modelClass
* @param array $multipleModels
* @return array
*/
public static function createMultiple($modelClass, $multipleModels = [])
{
$model = new $modelClass;
$formName = $model->formName();
$post = Yii::$app->request->post($formName);
$models = [];
if (! empty($multipleModels)) {
$keys = array_keys(ArrayHelper::map($multipleModels, 'id', 'id'));
$multipleModels = array_combine($keys, $multipleModels);
}
if ($post && is_array($post)) {
foreach ($post as $i => $item) {
if (isset($item['id']) && !empty($item['id']) && isset($multipleModels[$item['id']])) {
$models[] = $multipleModels[$item['id']];
} else {
$models[] = new $modelClass;
}
}
}
unset($model, $formName, $post);
return $models;
在年循环中你使用 $n
但输入名称里面有 $i
。所以你所有的输入都用 [0]
前缀初始化。这就是为什么在服务器端你只有最后一条记录。
只需将[{$i}]id
更改为[{$n}]id
,将[{$i}]year
更改为[{$n}]year
等等