如何给动态表单域 yii2 隐藏输入值
How to give hidden input value to dynamic form fields yii2
几天来我对一个问题感到困惑,我无法解决它。我有一个具有不同 classes 的表单,当用户单击添加按钮时,我想生成几个字段。我可以生成字段,但我无法通过那些动态生成的字段传递特定的 class_id
。我正在使用 this widget 以下是我生成动态字段的方式。
唯一涉及的模型是Registration
形式。除此之外,我只是触发查询并显示 data.One class 可以有多个 Registrations
Class Registration
is id
name class_id
firstname
lastname
这是我的查看代码
<?php if (!empty($data)) {
foreach($data as $event){
$modelsRegistration = [new Registration()];
DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
'widgetBody' => '.container-items'.$model['id'], // required: css class selector
'widgetItem' => '.item'.$model['id'], // required: css class
'limit' => 4, // the maximum times, an element can be added (default 999)
'min' => 0, // 0 or 1 (default 1)
'insertButton' => '.add-item'.$model['id'], // css class
'deleteButton' => '.remove-item'.$model['id'], // css class
'model' => $modelsRegistration[0],
'formId' => 'registration-form',
'formFields' => [
'firstname',
'lastname',
],
]);
?>
<div class="container">
<div class="row">
<div class="col-lg-9 col-md-9 col-sm-12 col-xs-12">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th class="col-lg-4 col-md-4 col-sm-4 col-xs-4">Class Name</th>
<th class="col-lg-2 col-md-2 col-sm-2 col-xs-2">Rounds</th>
</tr>
</thead>
<tbody>
<tr>
<td><?= $event['name'] ?></td>
<td><button type="button" id="<?= $event['id'] ?>" class="add-item<?=$model['id'] ?> btn btn-success btn-xs">Add Rounds <i
class="glyphicon glyphicon-plus"></i></button></td>
</tr>
</tbody>
</table>
<div class="row">
<div class="container-items<?=$model['id'] ?>"><!-- widgetBody -->
<?php foreach ($modelsRegistration as $i => $modelRegistration){
?>
<div class="item<?=$model['id'] ?>"><!-- widgetItem -->
<?php
// necessary for update action.
if (!$modelRegistration->isNewRecord) {
echo Html::activeHiddenInput($modelRegistration, "[{$i}]id");
}
?>
<div class="row">
<div class="col-sm-5">
<?= $form->field($modelRegistration, "[{$i}]firstname")->textInput(['maxlength' => true]) ?>
</div>
<div class="col-sm-5">
<?= $form->field($modelRegistration, "[{$i}]lastname")->textInput(['maxlength' => true]) ?>
<?= $form->field($modelRegistration, "[{$i}]class_id")->hiddenInput(['maxlength' => true])->label(false) ?>
</div>
<div class="col-sm-2">
<button type="button" class="remove-item<?=$model['id'] ?> btn btn-danger btn-xs"><i
class="glyphicon glyphicon-minus"></i></button>
</div>
</div>
<!-- .row -->
</div>
<?php } ?>
</div>
</div>
</div>
</div>
</div>
<?php
DynamicFormWidget::end();
} } ?>
如果您的视图只显示一个 class 的人(因此,添加更多人意味着他们都是相同的 class),您可以在 dynamicform
中仅使用first/last 命名字段并稍后在控制器中添加此 id_class
保存之前。
示例:
// load and validation of the model
// ...
foreach ($modelsRegistration as $modelRegistration) {
$modelRegistration->class_id = $myClassId;
if (! ($flag = $modelRegistration->save(false))) {
$transaction->rollBack();
break;
}
}
现在,如果您认为所有 class 的人都按部分划分,并且每个部分都有此添加按钮,我强烈建议您从那里删除此添加按钮并创建一个新部分仅用于添加人(select 选择 class)。
步骤:
- 在所有这些部分的 DynamicFormWidget class 中删除选项
insertButton
。并删除 html 按钮(将不再起作用)。
添加新部分,如下所示:
<?php
$newModelsRegistration = [new Registration()];
DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper_registration', // required: [A-z0-9_]
'widgetBody' => '.container-items-registration', // required: css class selector
'widgetItem' => '.item-registration', // required: css class
'limit' => 1, // the maximum times, an element can be added (default 999)
'min' => 0, // 0 or 1 (default 1)
'insertButton' => '.add-item-registration', // css class
'deleteButton' => '.remove-item-registration', // css class
'model' => $newModelsRegistration[0], //
'formId' => 'registration-form',
'formFields' => [
'firstname',
'lastname',
'class_id'
],
]);
?>
<div class="panel-body">
<button type="button" class="add-item-registration btn btn-success btn-sm pull-right margin-b-base">
<i class="glyphicon glyphicon-plus"></i> Add
</button>
<div class="clearfix"></div>
<div class="container-items-registration"><!-- widgetBody -->
<?php foreach ($newModelsRegistration as $i => $newModelRegistration): ?>
<div class="item-registration panel panel-default"><!-- widgetItem -->
<div class="panel-heading">
<h3 class="panel-title pull-left">Registration</h3>
<div class="pull-right">
<button type="button" class="remove-item-registration btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button>
</div>
<div class="clearfix"></div>
</div>
<div class="panel-body">
<div class="form-group row">
<div class="col-md-12">
<?= $form->field($newModelRegistration, "[{$i}]firstname")->textInput(['maxlength' => true]) ?>
<?= $form->field($newModelRegistration, "[{$i}]lastname")->textInput(['maxlength' => true]) ?>
<?= $form->field($newModelRegistration, "[{$i}]class_id")->dropDownList(
ArrayHelper::map(Class::find()->all(), 'id', 'name'),
['prompt' => 'Select']
) ?>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<?php DynamicFormWidget::end(); ?>
确保 limit 选项设置为 1
。因此,很容易避免人们试图在同一个 class 中添加过多的注册(如果我理解正确的话,每个 class 都有注册数量限制)。您可以在注册模型中添加一个新规则,并检查它是否已经达到 class 的限制。 dropDownList
.
中使用的 find()
方法也是如此
但是如果您仍然希望在同一个视图中添加所有这些添加按钮,您可以随时使用 js 为您添加该值。
示例:
// begin of form
// ...
<?= $form->field($modelRegistration, "[{$i}]class_id")->hiddenInput(['maxlength' => true, 'class' => 'form-control add-class-id', 'data-event' => $event['id']])->label(false) ?>
// end of form
<?php
$js = <<<JS
$('#registration-form').on('submit', function (e) {
$('.add-class-id').each(function() {
$(this).val($(this).attr('data-event'));
});
});
JS;
$this->registerJs($js);
?>
让我知道这些想法是否适合您。
几天来我对一个问题感到困惑,我无法解决它。我有一个具有不同 classes 的表单,当用户单击添加按钮时,我想生成几个字段。我可以生成字段,但我无法通过那些动态生成的字段传递特定的 class_id
。我正在使用 this widget 以下是我生成动态字段的方式。
唯一涉及的模型是Registration
形式。除此之外,我只是触发查询并显示 data.One class 可以有多个 Registrations
Class Registration
is id
name class_id
firstname
lastname
这是我的查看代码
<?php if (!empty($data)) {
foreach($data as $event){
$modelsRegistration = [new Registration()];
DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
'widgetBody' => '.container-items'.$model['id'], // required: css class selector
'widgetItem' => '.item'.$model['id'], // required: css class
'limit' => 4, // the maximum times, an element can be added (default 999)
'min' => 0, // 0 or 1 (default 1)
'insertButton' => '.add-item'.$model['id'], // css class
'deleteButton' => '.remove-item'.$model['id'], // css class
'model' => $modelsRegistration[0],
'formId' => 'registration-form',
'formFields' => [
'firstname',
'lastname',
],
]);
?>
<div class="container">
<div class="row">
<div class="col-lg-9 col-md-9 col-sm-12 col-xs-12">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th class="col-lg-4 col-md-4 col-sm-4 col-xs-4">Class Name</th>
<th class="col-lg-2 col-md-2 col-sm-2 col-xs-2">Rounds</th>
</tr>
</thead>
<tbody>
<tr>
<td><?= $event['name'] ?></td>
<td><button type="button" id="<?= $event['id'] ?>" class="add-item<?=$model['id'] ?> btn btn-success btn-xs">Add Rounds <i
class="glyphicon glyphicon-plus"></i></button></td>
</tr>
</tbody>
</table>
<div class="row">
<div class="container-items<?=$model['id'] ?>"><!-- widgetBody -->
<?php foreach ($modelsRegistration as $i => $modelRegistration){
?>
<div class="item<?=$model['id'] ?>"><!-- widgetItem -->
<?php
// necessary for update action.
if (!$modelRegistration->isNewRecord) {
echo Html::activeHiddenInput($modelRegistration, "[{$i}]id");
}
?>
<div class="row">
<div class="col-sm-5">
<?= $form->field($modelRegistration, "[{$i}]firstname")->textInput(['maxlength' => true]) ?>
</div>
<div class="col-sm-5">
<?= $form->field($modelRegistration, "[{$i}]lastname")->textInput(['maxlength' => true]) ?>
<?= $form->field($modelRegistration, "[{$i}]class_id")->hiddenInput(['maxlength' => true])->label(false) ?>
</div>
<div class="col-sm-2">
<button type="button" class="remove-item<?=$model['id'] ?> btn btn-danger btn-xs"><i
class="glyphicon glyphicon-minus"></i></button>
</div>
</div>
<!-- .row -->
</div>
<?php } ?>
</div>
</div>
</div>
</div>
</div>
<?php
DynamicFormWidget::end();
} } ?>
如果您的视图只显示一个 class 的人(因此,添加更多人意味着他们都是相同的 class),您可以在 dynamicform
中仅使用first/last 命名字段并稍后在控制器中添加此 id_class
保存之前。
示例:
// load and validation of the model
// ...
foreach ($modelsRegistration as $modelRegistration) {
$modelRegistration->class_id = $myClassId;
if (! ($flag = $modelRegistration->save(false))) {
$transaction->rollBack();
break;
}
}
现在,如果您认为所有 class 的人都按部分划分,并且每个部分都有此添加按钮,我强烈建议您从那里删除此添加按钮并创建一个新部分仅用于添加人(select 选择 class)。
步骤:
- 在所有这些部分的 DynamicFormWidget class 中删除选项
insertButton
。并删除 html 按钮(将不再起作用)。 添加新部分,如下所示:
<?php $newModelsRegistration = [new Registration()]; DynamicFormWidget::begin([ 'widgetContainer' => 'dynamicform_wrapper_registration', // required: [A-z0-9_] 'widgetBody' => '.container-items-registration', // required: css class selector 'widgetItem' => '.item-registration', // required: css class 'limit' => 1, // the maximum times, an element can be added (default 999) 'min' => 0, // 0 or 1 (default 1) 'insertButton' => '.add-item-registration', // css class 'deleteButton' => '.remove-item-registration', // css class 'model' => $newModelsRegistration[0], // 'formId' => 'registration-form', 'formFields' => [ 'firstname', 'lastname', 'class_id' ], ]); ?> <div class="panel-body"> <button type="button" class="add-item-registration btn btn-success btn-sm pull-right margin-b-base"> <i class="glyphicon glyphicon-plus"></i> Add </button> <div class="clearfix"></div> <div class="container-items-registration"><!-- widgetBody --> <?php foreach ($newModelsRegistration as $i => $newModelRegistration): ?> <div class="item-registration panel panel-default"><!-- widgetItem --> <div class="panel-heading"> <h3 class="panel-title pull-left">Registration</h3> <div class="pull-right"> <button type="button" class="remove-item-registration btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button> </div> <div class="clearfix"></div> </div> <div class="panel-body"> <div class="form-group row"> <div class="col-md-12"> <?= $form->field($newModelRegistration, "[{$i}]firstname")->textInput(['maxlength' => true]) ?> <?= $form->field($newModelRegistration, "[{$i}]lastname")->textInput(['maxlength' => true]) ?> <?= $form->field($newModelRegistration, "[{$i}]class_id")->dropDownList( ArrayHelper::map(Class::find()->all(), 'id', 'name'), ['prompt' => 'Select'] ) ?> </div> </div> </div> </div> <?php endforeach; ?> </div> </div> <?php DynamicFormWidget::end(); ?>
确保 limit 选项设置为
1
。因此,很容易避免人们试图在同一个 class 中添加过多的注册(如果我理解正确的话,每个 class 都有注册数量限制)。您可以在注册模型中添加一个新规则,并检查它是否已经达到 class 的限制。dropDownList
. 中使用的
find()
方法也是如此
但是如果您仍然希望在同一个视图中添加所有这些添加按钮,您可以随时使用 js 为您添加该值。
示例:
// begin of form
// ...
<?= $form->field($modelRegistration, "[{$i}]class_id")->hiddenInput(['maxlength' => true, 'class' => 'form-control add-class-id', 'data-event' => $event['id']])->label(false) ?>
// end of form
<?php
$js = <<<JS
$('#registration-form').on('submit', function (e) {
$('.add-class-id').each(function() {
$(this).val($(this).attr('data-event'));
});
});
JS;
$this->registerJs($js);
?>
让我知道这些想法是否适合您。