Open epulgaron opened 8 years ago
maybe please post your model, controller and view
here is my _form and my controller
_form
<?php
use yii\helpers\Html;
//use yii\widgets\ActiveForm;
use wbraganca\dynamicform\DynamicFormWidget;
use backend\models\Model;
use backend\models\Modelo01;
use backend\models\Modelo03;
use yii\helpers\ArrayHelper;
use kartik\widgets\Select2;
// use yii\jui\DatePicker;
use yii\helpers\Url;
use yii\bootstrap\ActiveForm;
use kartik\widgets\DatePicker;
use kartik\datecontrol\DateControl;
/* @var $this yii\web\View */
/* @var $model backend\models\Planificacion */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="planificacion-form">
<?php $form = ActiveForm::begin(['id' => 'dynamic-form','layout'=>'horizontal']); ?>
<?= $form->field($model, 'plan_cod')->textInput(['maxlength' => 255,'style'=>'width:250px;']) ?>
<?= $form->field($model, 'plan_desc')->textarea(['maxlength' => 5000,'style'=>'width:250px;']) ?>
<?= $form->field($model, 'plan_acc_extra')->checkbox(array('value' => 'Si', 'uncheck' => 'No')) ?>
<?= $form->field($model, 'orga_fk')->widget(Select2::classname(), [
'data' => ArrayHelper::map(\backend\models\Norganismos::find()->asArray()->all(), 'id_orga', 'orga_siglas'),
'options' => ['placeholder' => 'Seleccione un organismo','style'=>'width:250px;',
'onchange'=>'
$.get( "'.Url::toRoute('/planificacion/lists').'", { id: $(this).val() } )
.done(function( data ) {
$( "#'.Html::getInputId($model, 'entidad_fk').'" ).html( data );
}
);
'],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
<?= $form->field($model, 'entidad_fk')->widget(Select2::classname(), [
'data' => ArrayHelper::map(\backend\models\Entidad::find()->asArray()->all(), 'id_entidad', 'entidad_nombre'),
'options' => ['placeholder' => 'Seleccione una entidad','style'=>'width:250px;'],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
<?= $form->field($model, 'tipo_accion_fk')->widget(Select2::classname(), [
'data' => ArrayHelper::map(\backend\models\Ntipoaccion::find()->asArray()->all(), 'id_tipo_accion', 'tipo_acc_desc'),
'options' => ['placeholder' => 'Seleccione un tipo de acción ','style'=>'width:250px;'],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
<?= $form->field($model, 'particularidades_fk')->widget(Select2::classname(), [
'data' => ArrayHelper::map(\backend\models\Nparticularidades::find()->asArray()->all(), 'id_particularidades', 'particularidades_desc'),
'options' => ['placeholder' => 'Seleccione una particularidad ','style'=>'width:250px;'],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
<?= $form->field($model, 'plan_fecha_inicio')->widget(DatePicker::classname(), [
'model' => $model,
'attribute' => 'plan_fecha_inicio',
'options'=>['placeholder'=>'Inserte una fecha','style'=>'width:250px;'],
'language' => 'es',
'pluginOptions' => [
'dateFormat' => 'yy-mm-dd',
],
//'dateFormat' => 'yyyy-MM-dd',
]); ?>
<?= $form->field($model, 'plan_fecha_fin')->widget(DatePicker::classname(), [
'model' => $model,
'attribute' => 'plan_fecha_fin',
'options'=>['placeholder'=>'Inserte una fecha','style'=>'width:250px;'],
'language' => 'es',
'pluginOptions' => [
'dateFormat' => 'yy-mm-dd',
],
//'dateFormat' => 'yyyy-MM-dd',
]); ?>
<?= $form->field($model, 'plan_cant_aut')->textInput(['style'=>'width:250px;']) ?>
<?= $form->field($model, 'plan_cant_dias')->textInput(['style'=>'width:250px;']) ?>
<?= $form->field($model, 'conclusion_fk')->widget(Select2::classname(), [
'data' => ArrayHelper::map(\backend\models\Nconclusiones::find()->asArray()->all(), 'id_conclusion', 'conclusion_desc'),
'options' => ['placeholder' => 'Seleccione un estado ','style'=>'width:250px;'],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
<?= $form->field($model, 'dic_fk')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'plan_reg_por')->textInput(['maxlength' => true]) ?>
<div class="row">
<div class="panel panel-default">
<div class="panel-heading"><h4><i class="glyphicon glyphicon-envelope"></i> Modelo 01</h4></div>
<div class="panel-body">
<?php DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
'widgetBody' => '.container-items', // required: css class selector
'widgetItem' => '.item', // required: css class
'limit' => 1, // the maximum times, an element can be cloned (default 999)
'min' => 0, // 0 or 1 (default 1)
'insertButton' => '.add-item', // css class
'deleteButton' => '.remove-item', // css class
'model' => $modelsModelo01[0],
'formId' => 'dynamic-form',
'formFields' => [
'plan_orden',
'modelo01_directiva',
'modelo01_fecha_ini_real',
'modelo01_fecha_fin_real',
'calificacion_fk',
'modelo01_cant_aud_real',
'modelo01_dias_real',
'modelo01_phc',
'modelo01_phd',
'dic_fk',
],
]); ?>
<div class="panel panel-default">
<div class="panel-heading">
<h4>
<i class="glyphicon glyphicon-envelope"></i>
<button type="button" class="add-item btn btn-success btn-sm pull-right"><i class="glyphicon glyphicon-plus"></i> Insertar</button>
</h4>
</div>
<div class="panel-body">
<div class="container-items"><!-- widgetBody -->
<?php foreach ($modelsModelo01 as $i => $modelModelo01): ?>
<div class="item panel panel-default"><!-- widgetItem -->
<div class="panel-heading">
<h3 class="panel-title pull-left"></h3>
<div class="pull-right">
<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">
<?php
// necessary for update action.
if (! $modelModelo01->isNewRecord) {
echo Html::activeHiddenInput($modelModelo01, "[{$i}]planificacion_fk");
}
?>
<?= $form->field($modelModelo01, "[{$i}]plan_orden")->textInput(['maxlength' => 255,'style'=>'width:250px;']) ?>
<div class="row">
<div class="col-sm-6">
<?= $form->field($modelModelo01, "[{$i}]modelo01_directiva")->textInput(['maxlength' => 255,'style'=>'width:250px;']) ?>
</div>
<div class="col-sm-6">
<?= $form->field($modelModelo01, "[{$i}]modelo01_fecha_ini_real")->widget(DatePicker::classname(), [
'model' => $modelModelo01,
'attribute' => 'modelo01_fecha_ini_real',
'options'=>['placeholder'=>'Inserte una fecha','style'=>'width:250px;'],
'language' => 'es',
'pluginOptions' => [
'dateFormat' => 'yyyy-MM-dd',
],
]);
?>
</div>
</div><!-- .row -->
<div class="row">
<div class="col-sm-4">
<?= $form->field($modelModelo01, "[{$i}]modelo01_fecha_fin_real")->widget(DatePicker::classname(), [
'model' => $modelModelo01,
'attribute' => 'modelo01_fecha_fin_real',
'options'=>['placeholder'=>'Inserte una fecha','style'=>'width:250px;'],
'language' => 'es',
'pluginOptions' => [
'dateFormat' => 'yyyy-MM-dd',
],
]); ?>
</div>
<div class="col-sm-4">
<?= $form->field($modelModelo01, "[{$i}]calificacion_fk")->widget(Select2::classname(), [
'data' => ArrayHelper::map(\backend\models\Ncalificaciones::find()->asArray()->all(), 'id_calificacion', 'calificacion_desc'),
'options' => ['placeholder' => 'Seleccione una calificación ','style'=>'width:250px;'],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
</div>
<div class="col-sm-4">
<?= $form->field($modelModelo01, "[{$i}]modelo01_cant_aud_real")->textInput(['style'=>'width:250px;']) ?>
</div>
</div><!-- .row -->
<div class="row">
<div class="col-sm-4">
<?= $form->field($modelModelo01, "[{$i}]modelo01_dias_real")->textInput(['style'=>'width:250px;']) ?>
</div>
<div class="col-sm-4">
<?= $form->field($modelModelo01, "[{$i}]modelo01_phc")->textInput(['style'=>'width:250px;']) ?>
</div>
<div class="col-sm-4">
<?= $form->field($modelModelo01, "[{$i}]modelo01_phd")->textInput(['style'=>'width:250px;']) ?>
</div>
</div><!-- .row -->
<div class="row">
<div class="col-sm-4">
<?= $form->field($modelModelo01, "[{$i}]dic_fk")->textInput(['maxlength' => 255,'style'=>'width:250px;']) ?>
</div>
</div><!-- .row -->
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div><!-- .panel -->
<?php DynamicFormWidget::end(); ?>
</div>
</div>
</div>
<div class="row">
<div class="panel panel-default">
<div class="panel-heading"><h4><i class="glyphicon glyphicon-envelope"></i> Modelo 03</h4></div>
<div class="panel-body">
<?php DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
'widgetBody' => '.container-items2', // required: css class selector
'widgetItem' => '.item2', // required: css class
'limit' => 999, // the maximum times, an element can be cloned (default 999)
'min' => 0, // 0 or 1 (default 1)
'insertButton' => '.add-item2', // css class
'deleteButton' => '.remove-item2', // css class
'model' => $modelsModelo03[0],
'formId' => 'dynamic-form',
'formFields' => [
'cargo_fk',
'medida_apl_fk',
'modelo03_cant',
'dic_fk',
],
]); ?>
<div class="panel panel-default">
<div class="panel-heading">
<h4>
<i class="glyphicon glyphicon-envelope"></i>
<button type="button" class="add-item2 btn btn-success btn-sm pull-right"><i class="glyphicon glyphicon-plus"></i> Insertar</button>
</h4>
</div>
<div class="panel-body">
<div class="container-items2"><!-- widgetBody -->
<?php foreach ($modelsModelo03 as $i => $modelModelo03): ?>
<div class="item2 panel panel-default"><!-- widgetItem -->
<div class="panel-heading">
<h3 class="panel-title pull-left"></h3>
<div class="pull-right">
<button type="button" class="remove-item2 btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button>
</div>
<div class="clearfix"></div>
</div>
<div class="panel-body">
<?php
// necessary for update action.
if (! $modelModelo03->isNewRecord) {
echo Html::activeHiddenInput($modelModelo03, "[{$i}]planificacion_fk");
}
?>
<div class="row">
<div class="col-sm-6">
<?= $form->field($modelModelo03, "[{$i}]cargo_fk")->textInput(['maxlength' => true]) ?>
</div>
<div class="col-sm-6">
<?= $form->field($modelModelo03, "[{$i}]medida_apl_fk")->textInput(['maxlength' => true]) ?>
</div>
</div><!-- .row -->
<div class="row">
<div class="col-sm-4">
<?= $form->field($modelModelo03, "[{$i}]modelo03_cant")->textInput(['maxlength' => true]) ?>
</div>
<div class="col-sm-4">
<?= $form->field($modelModelo03, "[{$i}]dic_fk")->textInput(['maxlength' => true]) ?>
</div>
</div><!-- .row -->
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div><!-- .panel -->
<?php DynamicFormWidget::end(); ?>
</div>
</div>
</div>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
controller
<?php
namespace backend\controllers;
use Yii;
use backend\models\Planificacion;
use backend\models\PlanificacionSearch;
use backend\models\Modelo01;
use backend\models\Modelo03;
use backend\models\Model;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;
use mPDF;
/**
* PlanificacionController implements the CRUD actions for Planificacion model.
*/
class PlanificacionController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
/**
* Lists all Planificacion models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new PlanificacionSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Planificacion model.
* @param string $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Planificacion model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Planificacion();
$modelsModelo01 = [new Modelo01];
$modelsModelo03 = [new Modelo03];
if ($model->load(Yii::$app->request->post()) /*&& $model->save()*/)
{
$modelsModelo01 = Model::createMultiple(Modelo01::classname());
\yii\base\Model::loadMultiple($modelsModelo01, Yii::$app->request->post());
$modelsModelo03 = Model::createMultiple(Modelo03::classname());
\yii\base\Model::loadMultiple($modelsModelo03, Yii::$app->request->post());
// validate all models
$valid = $model->validate();
$valid = \yii\base\Model::validateMultiple($modelsModelo01) && $valid;
$valid1 = $model->validate();
$valid1 = \yii\base\Model::validateMultiple($modelsModelo03) && $valid1;
if ($valid1 && $valid1 ) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
foreach ($modelsModelo01 as $modelModelo01)
{
$modelModelo01->planificacion_fk = $model->plan_cod;
if (! ($flag = $modelModelo01->save(false))) {
$transaction->rollBack();
break;
}
}
foreach ($modelsModelo03 as $modelModelo03)
{
$modelModelo03->planificacion_fk = $model->plan_cod;
if (! ($flag = $modelModelo03->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
return $this->redirect(['view', 'id' => $model->plan_cod]);
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
} else {
return $this->render('create', [
'model' => $model,
'modelsModelo01' => (empty($modelsModelo01)) ? [new Modelo01] : $modelsModelo01,
'modelsModelo03' => (empty($modelsModelo03)) ? [new Modelo03] : $modelsModelo03,
]);
}
}
/**
* Updates an existing Planificacion model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param string $id
* @return mixed
*/
public function actionUpdate($id)
{
//code
}
/**
* Deletes an existing Planificacion model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param string $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Planificacion model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param string $id
* @return Planificacion the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Planificacion::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
public function actionLists($id)
{
$rows = \app\models\Entidad::find()->where(['orga_fk' => $id])->all();
echo "<option>Selecione una entidad</option>";
if(count($rows)>0){
foreach($rows as $row){
echo "<option value='$row->id_entidad'>$row->entidad_nombre</option>";
}
}
else{
echo "<option>No existe ninguna entidad</option>";
}
}
}
Hi, Can you please try by creating seperate Model.php for both models & using that. e.g. Model.php for modelsModelo01 Model3.php for modelsModelo03
Thanks, Vishwas
vishwasrao thx for your answer i'll try it,but using just one dynamic form only save the last one too, i'm using your example project with po & po_items work fine but not for my models
Yes, I got the same problem.
Same problem. Any ideas?
Hello everyone, i have the same problem, i think there is something wrong in the view.
When i add new item, the name of the input is the same of the previous item. In this case only the last input field is posted. I show you what happen:
This is the first input:
<input id="testrelated2-0-prop_related2" class="form-control" name="TestRelated2[0][prop_related2]" type="text">
And this is the second added by button:
<input id="testrelated2-0-prop_related2" class="form-control" name="TestRelated2[0][prop_related2]" type="text">
How you can see, the index of TestRelated2[] is always 0. Can anyone explain me how to increment this index? Thanks in advance
@wbraganca @ToRvaLDz Can you help me?
I have create DynamicFormWidget. the field has qty, price, and subtotal. I have create js function like this `$js = "
function countSubtotal(i){ alert('dfsdg' ); } ";
$this->registerJs($js);`
It call from this field
<div class="col-sm-4 col-md-2"> <?= $form->field($detail, "[{$i}]qty") ->textInput(['maxlength' => true, 'id' => '_qty', 'onkeyup' => "countSubtotal({$i})"]) ?> </div>
But it always show an error in console like this ---Uncaught ReferenceError: countSubtotal is not defined at HTMLInputElement.onkeyup Can you help me to solve this?
You have to register the js in Head,
$this->registerJs($js, View::POS_HEAD);
On 11 Apr 2018 at 08:34, <mia29 notifications@github.com> wrote:
I have create DynamicFormWidget. the field has qty, price, and subtotal. I have create js function like this `$js = "
function countSubtotal(i){ alert('dfsdg' ); } ";
$this->registerJs($js);`
It call from this field
But it always show an error in console like this ---Uncaught ReferenceError: countSubtotal is not defined at HTMLInputElement.onkeyup Can you help me to solve this?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/wbraganca/yii2-dynamicform/issues/103#issuecomment-380342929, or mute the thread https://github.com/notifications/unsubscribe-auth/ABkUdJTG8SNLAUthkDcc46ZbswQrp2nbks5tnaPZgaJpZM4GbRwz .
Thanks the advice. The problem has been solve. Here is the console log error
---update?id=21:38 Uncaught TypeError: Cannot read property 'value' of null
at countSubtotal (update?id=21:38) at HTMLInputElement.onkeyup (update?id=21:212)
countSubtotal @ update?id=21:38
onkeyup @ update?id=21:212
Here i have a new problem from this issue. When I add the row. It seems DinamicForm generated new id automatically base the row-index. I can I catch this id to put in the function. Is there other way to get subtotal in this widget?
On Wed, Apr 11, 2018 at 2:26 PM, ToRvaLDz notifications@github.com wrote:
You have to register the js in Head,
$this->registerJs($js, View::POS_HEAD);
On 11 Apr 2018 at 08:34, <mia29 notifications@github.com> wrote:
I have create DynamicFormWidget. the field has qty, price, and subtotal. I have create js function like this `$js = "
function countSubtotal(i){ alert('dfsdg' ); } ";
$this->registerJs($js);`
It call from this field
= $form->field($detail, "[{$i}]qty") ->textInput(['maxlength' => true, 'id' => '_qty', 'onkeyup' => "countSubtotal({$i})"]) ?>But it always show an error in console like this ---Uncaught ReferenceError: countSubtotal is not defined at HTMLInputElement.onkeyup Can you help me to solve this?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/wbraganca/yii2-dynamicform/issues/103# issuecomment-380342929, or mute the thread https://github.com/notifications/unsubscribe-auth/ ABkUdJTG8SNLAUthkDcc46ZbswQrp2nbks5tnaPZgaJpZM4GbRwz .
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/wbraganca/yii2-dynamicform/issues/103#issuecomment-380354648, or mute the thread https://github.com/notifications/unsubscribe-auth/ALly76M19xBe2QXjMkTOR9Bys0ChEzBfks5tnbAvgaJpZM4GbRwz .
when i insert multiples values in my dynamic form only store in the database the last one , why this happen? sorry for my english