Yii2 Dynamic Data is not loading on all DepDrop in dynamic field - php

I'm trying to create a dynamic form for one of my projects. I used wbraganca's Dynamic From to achieve this. In my dynamic form I have a DepDrop to show subcategories list from a parent category.
My _form.php
<?php $form = ActiveForm::begin(['id' => 'dynamic-form']); ?>
<div class="row">
<div class="col-md-6">
<?= $form->field($model, 'rq_job_no')->textInput() ?>
</div>
<div class="col-md-6">
<?= $form->field($model, 'rq_req_date')->widget(\yii\jui\DatePicker::class, [
//'language' => 'ru',
'dateFormat' => 'php:Y-m-d',
'clientOptions' => [
'changeMonth' => true,
'changeYear' => true,
'showButtonPanel' => true,
'yearRange' => '1990:2030',
'minDate' => date('Y-m-d'),
],
'options' => ['class' => 'form-control', 'readOnly' => true, 'placeholder' => 'Enter the Item Required Date'],
]) ?>
</div>
<div class="col-md-6">
<?= $form->field($model, 'rq_priority_type')->dropDownList(['urgent' => 'Urgent', 'normal' => 'Normal'], ['prompt' => '--Select Priority Type--']) ?>
</div>
<div class="col-md-6">
</div>
<div class="col-md-6">
<?= $form->field($model, 'rq_approval_type')->dropDownList([1 => ' Approved Vendor Needed', 2 => 'Approved Submitted Needed', 3 => 'Warranty Certificate to be Collected', 4 => 'Long Lead Material',], ['prompt' => '--Select Type of Approval--']) ?>
<?php echo $form->field($model, 'rq_cat_type')->widget(Select2::class, [
'data' => $catData,
'options' => ['placeholder' => '--Select Request Type--', 'class' => 'form-control'],
'pluginOptions' => [
'allowClear' => true
],
]);
?>
</div>
<div class="col-md-12">
<?= $form->field($model, 'rq_remarks')->textarea(['rows' => 6]) ?>
</div>
</div>
<!-- code for dynamic form -->
<div class="panel panel-default">
<div class="panel-heading">
<h4><i class="glyphicon glyphicon-envelope"></i> Request Items</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' => 10, // the maximum times, an element can be cloned (default 999)
'min' => 1, // 0 or 1 (default 1)
'insertButton' => '.add-item', // css class
'deleteButton' => '.remove-item', // css class
'model' => $modelsAddress[0],
'formId' => 'dynamic-form',
'formFields' => [
'item_name',
'item_qty',
'item_unit',
],
]); ?>
<div class="container-items">
<!-- widgetContainer -->
<?php foreach ($modelsAddress as $i => $modelAddress) : ?>
<div class="item panel panel-default">
<!-- widgetBody -->
<div class="panel-heading">
<h4 class="panel-title pull-left">Items</h4>
<div class="pull-right">
<button type="button" class="add-item btn btn-success btn-xs"><i class="fa fa-plus"></i></button>
<button type="button" class="remove-item btn btn-danger btn-xs"><i class="fa fa-minus"></i></button>
</div>
<div class="clearfix"></div>
</div>
<div class="panel-body">
<?php
// necessary for update action.
if (!$modelAddress->isNewRecord) {
echo Html::activeHiddenInput($modelAddress, "[{$i}]rt_id");
}
?>
<?php //$form->field($modelAddress, "[{$i}]rt_item")->textInput(['maxlength' => true]) ?>
<?= $form->field($modelAddress, "[{$i}]rt_item")->widget(DepDrop::class, [
'options' => ['id' => 'reqitems-'.$i.'-rt_item'],
'pluginOptions' => [
'depends' => ['requests-rq_cat_type'],
'placeholder' => '--Select Location--',
'url' => Url::to(['/requests/subcat'])
]
]); ?>
<div class="row">
<div class="col-sm-6">
<?= $form->field($modelAddress, "[{$i}]rt_qty")->textInput(['maxlength' => true]) ?>
</div>
<div class="col-sm-6">
<?= $form->field($modelAddress, "[{$i}]rt_unit")->textInput(['maxlength' => true]) ?>
</div>
</div><!-- .row -->
</div>
</div>
<?php endforeach; ?>
</div>
<?php DynamicFormWidget::end(); ?>
</div>
</div>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
My Controller action to load the subcategory
public function actionSubcat()
{
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$out = [];
if (isset($_POST['depdrop_parents'])) {
$parents = $_POST['depdrop_parents'];
if ($parents != null) {
$cat_id = $parents[0];
$out = self::getSubCatList($cat_id);
return ['output' => $out, 'selected' => ''];
}
}
return ['output' => 'No Category Available', 'selected' => ''];
}
public function getSubCatList($cat_id)
{
$output = [];
$category = Item::find()->where(['item_category' => $cat_id])->orderBy('item_title')->all();
if (empty($category)) {
$output[] = ['id' => '0', 'name' => 'No Category Available'];
} else {
foreach ($category as $cat) {
$output[] = ['id' => $cat->item_id, 'name' => $cat->item_title];
}
}
return $output;
}
My problem is that DepDrop is only loading the subcategory for the first loop of the dynamic form and leaves the rest loop blank. Attached screenshot for your reference.
Can anyone help by pointing what I am missing or how can I achieve my requirement.

Related

yii2 display error msg

displaying msg on above the line, how to display error msg below line. I have added below view page. any suggestions.
below is view page
<div class="ibox-content bwform clearfix" id="bwcart">
<h2><b>Profile</b><div id="loader"></div></h2>
<?php $form = ActiveForm::begin(
['action' =>'',
'options' => ['method'=>'post','class'=>'form-horizontal','id'=>'profile-form','enctype'=>'multipart/form-data'],
'fieldConfig' => [
'template' => "{label}<div class=\"col-md-12\">{input}\n{error}</div>",
'labelOptions' => ['class' => 'col-lg-12 control-label p-l-0'],
],
'enableAjaxValidation'=>true,'enableClientValidation' => true,'validateOnSubmit'=>true]); ?>
<div class="box-2">
<div class="input input--hoshi <?=$prefilled ?>">
<?= $form->field($model, 'firstname')->textInput(array('placeholder' => '','class'=>'input__field input__field--hoshi'))->label(false); ?>
<label class="input__label input__label--hoshi input__label--hoshi-color-3" for="first_name">
<span class="input__label-content input__label-content--hoshi">First Name</span>
</label>
</div>
</div>
<div class="box-2">
<div class="input input--hoshi <?=$prefilled ?>">
<?= $form->field($model, 'lastname')->textInput(array('placeholder' => '','class'=>'input__field input__field--hoshi'))->label(false); ?>
<label class="input__label input__label--hoshi input__label--hoshi-color-3" for="first_name">
<span class="input__label-content input__label-content--hoshi">Last Name</span>
</label>
</div>
</div>
You can use this solution:
<?php $form = ActiveForm::begin(
['action' =>'',
'options' => ['method'=>'post','class'=>'form-horizontal','id'=>'profile-form','enctype'=>'multipart/form-data'],
'fieldConfig' => [
//modify this
'template' => "{label}<div class='col-md-12'>{input}</div><div class='col-md-12 '>{error}</div>",
'labelOptions' => ['class' => 'col-lg-12 control-label p-l-0'],
],
'enableAjaxValidation'=>true,'enableClientValidation' => true,'validateOnSubmit'=>true]); ?>
So your error tag is positioned
under your input tag.
Your error attribute should not be in your col-md-12 class div.
So it should be like this:
<?php $form = ActiveForm::begin(
['action' =>'',
'options' => ['method'=>'post','class'=>'form-horizontal','id'=>'profile-form','enctype'=>'multipart/form-data'],
'fieldConfig' => [
'template' => "{label}<div class=\"col-md-12\">{input}</div>{error}",
'labelOptions' => ['class' => 'col-lg-12 control-label p-l-0'],
],
'enableAjaxValidation'=>true,'enableClientValidation' => true,'validateOnSubmit'=>true]); ?>

Yii2 - How make a dropdownlist with a database and show fields from the selected value

With Yii2, I'm trying to make a dropdownlist from a database (already done) but show the fields from the selected value.
Example :
Table :
Immeuble
Immeuble_id
Immeuble_name
Immeuble_propetary
Immeuble_color
This is my code :
Model :
class Imm extends ActiveRecord{
public static function tableName()
{
return 'i10';
}
public function attributeLabels(){
return array(
'N_NUME_IMME' => 'N_NUME_IMME',
);
}
}
Controller :
public function actionImm($id)
{
$model = new Imm(); // envoyer le model sur imm.php
$query = Imm::find(); // trouver le model
$model = $this->loadModel($id);
$imm = Imm::find()->where(['N_NUME_IMME' => $id])->one();
$items = ArrayHelper::map(Imm::find()->all(), 'N_NUME_IMME', 'L_NOM1_COMP');
return $this->render('imm',['imm'=>$model,'imm'=>$imm, 'items'=>$items]);
}
views :
<h1>Immeuble : </h1>
<?php $form = ActiveForm::begin([
'id' => 'login-form',
'layout' => 'horizontal',
'fieldConfig' => [
'template' => "{label}\n<div class=\"col-lg-6\">{input}</div>\n<div class=\"col-lg-6\">{error}</div>",
'labelOptions' => ['class' => 'col-lg-1 control-label'],
],
]); ?>
<div class="form-group">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<?= Html::activeDropDownList($imm, 'N_NUME_IMME',$items, ['class' => 'form-control', 'name' => 'liste_immeubles'])?>
</div>
</div>
<div class="form-group">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<?= Html::submitButton('Chaufferie', ['class' => 'btn btn-primary', 'name' => 'bouton_']) ?>
<?= Html::submitButton('Locaux communs', ['class' => 'btn btn-primary', 'name' => 'bouton_d']) ?>
<?= Html::submitButton('Parking/Amenagements Ext', ['class' => 'btn btn-primary', 'name' => 'bouton_donnees_generales']) ?>
<?= Html::submitButton('Facades/Toitures', ['class' => 'btn btn-primary', 'name' => 'bouton_don']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
<h2>Donnees Generales</h2>
<div class="col-lg-6">
<?= $form->field($imm, 'C_MODE_CHAU')->textInput(['autofocus' => false]) ?>
</div>
<div class="col-lg-6">

Floating of labels not working in yii2

i have created a form in yii2 basic and used material design to enable floating labels but its labels are not floating above when i click the input field. Here is my code for form.
<?php
$form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]);
?>
<div class="panel-body container-fluid">
<div class="row">
<div class="col-md-12">
<div class="col-xs-6 col-lg-6">
<div class="form-group form-material floating" data-plugin="formMaterial">
<?= $form->field($model, 'corporation_name',[ 'labelOptions' => [ 'class' => 'floating-label' ]])->textInput(['maxlength' => true, 'class' => 'form-control'])->label('Corporation Name') ?>
</div>
<div class="form-group form-material floating" data-plugin="formMaterial">
<?= $form->field($model, 'user_first_name',[ 'labelOptions' => [ 'class' => 'floating-label' ]])->textinput(['class' => 'form-control'])->label('Corporation Admin'); ?>
</div>
</div>
<div class="col-xs-6 col-lg-6">
<div class="form-group form-material floating" data-plugin="formMaterial">
<div class="example"> <b role="presentation"></b>
<?= $form->field($model, 'corporation_status')->dropDownList(Yii::$app->appstatues->status, ['prompt' => 'Status'])->label(false); ?>
</div>
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-group form-material floating m-t-27" data-plugin="formMaterial">
<?= Html::submitButton('Search', ['class' => 'btn-primary btn waves-effect waves-light waves-effect']) ?>
<?= Html::resetButton('Reset', ['class' => 'btn-primary btn waves-effect waves-light waves-effect', 'onclick' => 'location.href = "/admin/corporations";']) ?>
</div>
</div>
</div>
</div>
<?php ActiveForm::end(); ?>
</div>
can anybody tell what am i doing wrong?
Have you look in inspect element?
If I understand you well...
Try to remove divs around input form. When you bind form for model you already have divs around input. Try this...
<?= $form->field($model, 'ime',['options'=>['class'=>'form-group form-material floating', 'data-plugin'=> 'formMaterial'], 'labelOptions' => [ 'class' => 'floating-label' ]])->textInput(['maxlength' => true, 'class' => 'form-control'])->label('Corporation Name') ?>

save message is not displayed in yii form

I have 2 forms, the save message is displayed for 1st, but don't display on another.
1st view give confirmation message but the 2nd don't, the data is saved in tables.
gist for codes
in the above gist, you can see the controller and views for both.
You can used this code for you solution. Check set Message code in below.
view.php
<div class="ibox">
<div class="ibox-heading">
<div class="ibox-title">
<h3><?PHP echo(($model->isNewRecord) ? 'Create Fee Structure' : 'Update Fee Structure'); ?></h3>
</div>
</div>
<!-- /.box-header -->
<!-- Set Message start -->
<?php if(Yii::app()->user->hasFlash('confirm')):?>
<div class="info">
<?php echo Yii::app()->user->getFlash('confirm'); ?>
</div>
<?php endif; ?>
<!-- Message end -->
<!-- form start -->
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'fee_structure_form',
'enableAjaxValidation' => true,
'htmlOptions' => array(
'enctype' => 'multipart/form-data',
'role' => 'form'
),
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange' => true,
'validateOnType' => false,
),
)); ?>
<div class="ibox-content">
<div class="row">
<div class="col-md-6 col-xs-12">
<div class="form-group">
<?PHP
echo $form->label($model, 'class_id');
echo $form->dropDownList($model, 'class_id', $studentClasses,array('placeholder' => 'Class', 'class' => 'form-control'));
echo $form->error($model,'class_id');
?>
</div>
<div class="form-group">
<?PHP
echo $form->label($model, 'fee_type');
echo $form->dropDownList($model, 'fee_type', $feeTypes, array('placeholder' => 'Fee Type', 'class' => 'form-control'));
echo $form->error($model,'fee_type');
?>
</div>
<div class="form-group">
<?PHP
echo $form->label($model, 'amount');
echo $form->textField($model, 'amount', array('placeholder' => 'Amount', 'class' => 'form-control'));
echo $form->error($model,'amount');
?>
</div>
<div class="form-group">
<?PHP
echo $form->label($model, 'remarks');
echo $form->textField($model, 'remarks', array('placeholder' => 'Remarks', 'class' => 'form-control'));
echo $form->error($model,'remarks');
?>
</div>
</div>
</div>
</div>
<div class="ibox-footer col-md-12 col-xs-12" style="margin-bottom: 60px;">
<button class="btn col-md-3 col-xs-12 btn-primary pull-left" type="submit">
<?PHP echo Definations::SAVEFORM; ?>
</button>
</div>
<?PHP
$this->endWidget();
?>
</div>

How to update another part of the page(using PJAX)?

I have this code in layouts/main.php file:
<div class="wrap">
<? \yii\widgets\Pjax::begin(['id' => 'nav_pjax'])?>
<?php
NavBar::begin([
'brandLabel' => 'Название компании',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'id' => 'ww',
'class' => 'navbar-inverse navbar-relative',
],
]);
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right', 'id' => 'ww'],
'items' => User::getMenuItemsByRoleUser(!Yii::$app->user->isGuest ? Yii::$app->user : false,Yii::$app->user->isGuest)
]);
NavBar::end();
?>
<? \yii\widgets\Pjax::end()?>
<div class="container" id="content_inner">
<? \yii\widgets\Pjax::begin(['timeout' => '3000'])?>
<?= Breadcrumbs::widget([
'homeLink'=>[
'label' => 'Главная', // required
'url' => \yii\helpers\Url::home(), // optional, will be processed by Url::to()
],
'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
]) ?>
<?= $content ?>
<? \yii\widgets\Pjax::end()?>
</div>
When I click into navbar link, the server returns a content, which I want to put into div class="container" id="content_inner". How I can do that? Or I should refresh the page completely?
You don't need two pjax widget, you could simply try :
1) Update just content
<div class="wrap">
<?php /* navbar */ ?>
<div class="container" id="content_inner">
<?php \yii\widgets\Pjax::begin([
'id' => 'nav_pjax',
'timeout' => '3000',
'linkSelector' => '.navbar-nav a', // use pjax on navbar links
]); ?>
<?php /* breadcrumb and content */ ?>
<?php \yii\widgets\Pjax::end(); ?>
</div>
</div>
Read more about linkSelector.
2) Update navbar and content
<div class="wrap">
<?php \yii\widgets\Pjax::begin([
'id' => 'nav_pjax',
'timeout' => '3000',
]); ?>
<?php /* navbar */ ?>
<div class="container" id="content_inner">
<?php /* breadcrumb and content */ ?>
</div>
<?php \yii\widgets\Pjax::end(); ?>
</div>

Categories