I have following code in my model:
public function getData($property)
{
$data = array(
'a_4_1' => array(
'RUB',
'USD',
'JPY',
),
);
return $data[$property];
}
And in my view:
<?= $form->labelEx($model, 'a_4_1', array('class' => 'col-xs-12 col-sm-2 control-label')) ?>
<div class="col-xs-12 col-sm-3">
<?= $form->dropDownList($model, 'a_4_1',$model->getData('d_4_1'), array('class' => 'form-control')) ?>
<?= $form->error($model, 'a_4_1') ?>
</div>
When I save it to the database, it saves data in the integer format (e.g 1,2,..)
I need to save array elements' names to the database(e.g RUB, USD)(not integer numbers). How can I do it?
use array_combine() to change key value of your array like below:
array_combine($model->getData('d_4_1'),$model->getData('d_4_1'))
<?= $form->labelEx($model, 'a_4_1', array('class' => 'col-xs-12 col-sm-2 control-label')) ?>
<div class="col-xs-12 col-sm-3">
<?= $form->dropDownList($model, 'a_4_1',array_combine($model->getData('d_4_1'),$model->getData('d_4_1')), array('class' => 'form-control')) ?>
<?= $form->error($model, 'a_4_1') ?>
</div>
Related
I started to learn Laravel (nice framework), and being more and more familiar with it. But I arrive at a point where I don't understand why and how..
I generate a PDF from one of my view. The view is just composed by 3 {!! Form::text , which are filled by Varchar (length:2000) coming from my database.
I well generate the PDF but i can't understand why the text inside goes totally out of the the {!! Form::text in the .pdf-file.
I can't understand why the text stand inside the {!! Form::text for the first lines, but at the last one, the text will go out of the box and keep being writing until it touch the right margin of the pdf, and so diseapper..
I try to play around with {!! Form::text or {!! Form::textarea: same results..
I try to play around with " 'rows' => '15', 'cols' => '100'," parameters: same results...
Does someone might have an idea on why the text doesn't want to stay inside its {!! Form::textarea for description/sentiment and position forms?
My view:
#extends('template')
#section('contenu')
<div class="container">
<div class="jumbotron">
<h2> PTS | <em><strong> Weekly Comments</strong></h2>
<h3><strong>week: {{$week_number}}</strong></h3>
<br>
</div>
#foreach($comments as $comment)
<div class="row">
<div class="panel panel-primary">
<div class="panel-heading" style="text-align:center">
<h3{!! Form::textarea('desk_name', $comment->desk_name, array( 'class'=>'form-control','type' => 'text', 'rows'
=> '5', 'cols' => '5')) !!}</h3>
</div>
</div>
</div>
<br/>
<div class="row">
<div class="col-md-4">
<h3> P&L Weekly | </h3>
{!! Form::textarea('pnl_wtd', $comment->pnl_wtd, array( 'class'=>'form-control','type' => 'text', 'rows' => '5',
'cols' => '5')) !!}
</div>
<div class="col-md-4">
<h3> P&L MTD | </h3>
{!! Form::textarea('pnl_mtd', $comment->pnl_mtd, array('class'=>'form-control','type' => 'text', 'rows' => '5',
'cols' => '5')) !!}
</div>
<div class="col-md-4">
<h3> P&L YTD | </h3>
{!! Form::textarea('pnl_ytd', $comment->pnl_ytd, array( 'class'=>'form-control','type' => 'text', 'rows' => '5',
'cols' => '5')) !!}
</div>
</div>
<br>
<br>
<div class="row">
<div class="panel panel-warning">
<div class="panel-heading" style="text-align:center">
<h3>Past Week Summary</h3>
</div>
<div class="panel-body">
<div class="col-lg-8">
{!! Form::textarea('description', $comment->description, ['class' => 'textarea']) !!}
</div>
</div>
</div>
</div>
<div class="row">
<div class="panel panel-warning">
<div class="panel-heading" style="text-align:center">
<h3>Sentiment</h3>
</div>
<div class="panel-body">
<div class="col-lg-8">
{!! Form::textarea('sentiment', $comment->sentiment, ['class' => 'form-control', 'type' => 'text', 'rows' =>
'100000', 'cols' => '100000']) !!}
</div>
</div>
</div>
</div>
<div class="row">
<div class="panel panel-warning">
<div class="panel-heading" style="text-align:center">
<h3>Positions</h3>
</div>
<br>
</div>
</div>
<div class="container" id="posi">
{!! Form::textarea('position', $comment->position, ['class' => 'form-control', 'style'=>'resize: none']) !!}
</div>
#endforeach
#stop
The function from my controller:
public function pdfWeekly( $week_number ) {
$picuser = Auth::user();
$comments = Comment::where( 'week_number', '=', $week_number )->get();
$flag = 'pdf';
$pdf = PDF::loadView( 'tr.wc.emailPdf', compact(
'picuser',
'flag',
'week_number',
'comments'
) );
$name = "Principale Trading- Week #" . $week_number . ".pdf";
return $pdf->download( $name );
}
Best Regards.
The pdf use native html layouts. So your framework classes and expressions wont work here.
Try making a divv with width 710px and put you data in that div, it won't flow out.
I have a solution for this it is replace input or textarea to a tag like a textarea-to-text before save. I do:
var $html = $('html').clone();
$($html).find('textarea').replaceWith(function () {
$element = $("<textarea-to-text>").text($(this).val());`
$.each(this.attributes, function (i, attribute) {
$element.attr(attribute.name, attribute.value);
});
return $element;
});`
How do I create a dynamic list of the same form and show it on the view on Yii2 (it can be viewed as a list of the same object with different information) Thanks.
I have the following form on /frontend/views/site/example.php
That form I want to put it on a list.
<div class="site-example">
<h1><?= Html::encode($this->title) ?></h1>
<p>Example of a list:</p>
<div class="row">
<div class="col-lg-5">
<?php $form = ActiveForm::begin(['id' => 'form-example']); ?>
<?= $form->field($model, 'email')->textInput(['readonly' => true, 'value' => $email]) ?>
<?= $form->field($model, 'lastname')->textInput(['readonly' => true, 'value' => $lastname]) ?>
<?= $form->field($model, 'phone')->textInput(['readonly' => true, 'value' => $phone]) ?>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>
Assuming this is your controller.
<?php
namespace frontend\controllers;
use Yii;
use yii\base\Model;
use yii\web\Controller;
use frontend\models\YourForm;
class SiteController extends Controller
{
public function actionYourAction()
{
$forms = [new YourForm, new YourForm, new YourForm];
return $this->render('example', [
'forms' => $forms
]);
}
}
Then your view could be below
<div class="site-example">
<h1><?= Html::encode($this->title) ?></h1>
<p>Example of a list:</p>
<div class="row">
<div class="col-lg-5">
<?php foreach ($forms as $index => $form): ?>
<?php $form = ActiveForm::begin(['options' => ['id' => "form-example-$index"]]); ?>
<?= $form->field($form, "[$index]email")->textInput(['readonly' => true, 'value' => $form->email]) ?>
<?= $form->field($form, "[$index]lastname")->textInput(['readonly' => true, 'value' => $form->lastname]) ?>
<?= $form->field($form, "[$index]phone")->textInput(['readonly' => true, 'value' => $form->phone]) ?>
<?php ActiveForm::end(); ?>
<?php endforeach; ?>
</div>
</div>
</div>
I have a page which consist of 5 tab. 1st and 2nd tab is just normal filling form while my 3rd,4th and 5th tab is the index of other index page (which I can create update and delete the data). Below is the code for the form.
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use kartik\tabs\TabsX;
use kartik\date\DatePicker;
use frontend\models\OpStates;
use frontend\models\OpContact;
use frontend\models\OpCountries;
use frontend\models\OpClient;
use frontend\models\OpUnit;
use frontend\models\OpMaintenanceCharges;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
$this->title = 'Change Profile';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-changedetails">
<?php $form = ActiveForm::begin(['id'=>'changedetails-form',]); ?>
<?= TabsX::widget([
'position' => TabsX::POS_ABOVE,
'align' => TabsX::ALIGN_LEFT,
'items' => [
[
'label'=>'Client Details',
'content'=>
'<div class="row">
<div class="col-md-12">'.
$form->field($model, "client_code")->textInput(["maxlength" => true]).'
</div>
</div>
<div class="row">
<div class="col-md-12">'.
$form->field($model, "contact_id")->dropDownList(ArrayHelper::map(OpContact::find()->all(),'id','code')).'
</div>
</div>
<div class="row">
<div class="col-md-12">'.
$form->field($model, "unit_id")->dropDownList(ArrayHelper::map(OpUnit::find()->all(),'id','code')).'
</div>
</div>
<div class="row">
<div class="col-md-12">'.
$form->field($model, 'country_id')->dropDownList(ArrayHelper::map(OpCountries::find()->all(),'id','name'),
[
'prompt' => 'Select Country',
'onchange' => '
$.post("index.php?r=op-states/lists&id='.'" + $(this).val(), function(data){
$("select#opclient-states_id").html(data);
});'
]).'
</div>
</div>
<div class="row">
<div class="col-md-12">'.
$form->field($model, 'states_id')->dropDownList(ArrayHelper::map(OpStates::find()->all(),'id','state_name'),
[
'prompt' => 'Select States',
]).'
</div>
</div>
<div class="row">
<div class="col-md-6">'.
$form->field($model, 'city')->textInput(['maxlength' => true]).'
</div>
<div class="col-md-6">'.
$form->field($model, 'postcode')->textInput().'
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-1 col-md-offset-11">'.
Html::submitButton('Save',['class'=>'pull-right btn btn-primary','style'=>'width:70px; height:40px;','name'=>'button1']).'
</div>
</div>
</div>
'
],
[
'label'=>'Client Details 2',
'content'=>
' <div class="row">
<div class="col-md-12">'.
$form->field($model, "charge_interest")->dropDownList(['Yes'=>'Yes','No'=>'No']).'
</div>
</div>
<div class="row">
<div class="col-md-12">'.
$form->field($model, 'interest_effective_date')->widget(DatePicker::classname(), [
'pluginOptions' => [
'autoclose'=>true,
'format' => 'yyyy-mm-dd',
'todayHighlight' => true,
'todayBtn' => true,
]
]).'
</div>
</div>
<div class="row">
<div class="col-md-12">'.
$form->field($model, "charge_reminder")->dropDownList(['Yes'=>'Yes','No'=>'No']).'
</div>
</div>
<div class="row">
<div class="col-md-12">'.
$form->field($model, "external_debtor_code")->textInput().'
</div>
</div>
<div class="row">
<div class="col-md-12">'.
$form->field($model, "mailing_address1")->textInput().'
</div>
</div>
<div class="row">
<div class="col-md-12">'.
$form->field($model, "mailing_address2")->textInput().'
</div>
</div>
<div class="row">
<div class="col-md-12">'.
$form->field($model, "mailing_address3")->textInput().'
</div>
</div>
<div class="row">
<div class="col-md-12">'.
$form->field($model, "mailing_address4")->textInput().'
</div>
</div>
'
],
[
'label'=>'Maintenance Charges',
'content' => $this->render('//op-maintenance-charges/index', [
'model'=>$model,
'dataProviderMainCharges' => $dataProviderMainCharges,
]),
],
[
'label'=>'Misc Charges',
'content' => $this->render('//op-misc-charges/index', [
'model'=>$model,
'dataProviderMiscCharges' => $dataProviderMiscCharges,
]),
],
[
'label'=>'Occupier Details',
'content' => $this->render('//op-occupier/index', [
'model'=>$model,
'dataProviderOccupier' => $dataProviderOccupier,
]),
],
]
]); ?>
<?php ActiveForm::end(); ?>
The problem is the 1st index page is working normally. Click to see - Working normally
The 2nd index page n 3rd index cannot work. The form wont even come out. The screen just become darker and I cant click anything like this Click to see - Cant Work
Anyone can help or have any idea on how to fix it? Thanks
I am working on yii2 bootstrap active form and I need to keep multiple inputs in one form group .
like
<div class="form-group field-phn required">
<label class="control-label" >Home Phone</label>
<select id="dialCode" class="form-control" name="AddPatientForm[home_dial_code]">
<option value="2">355,ALB</option>
<option value="3">213,DZA</option>
<option value="6">244,AGO</option>
<option value="224">971,ARE</option>
</select>
<input type="text" id="home_phn" class="form-control" name="AddPatientForm[home_phn]">
<p class="help-block help-block-error"></p>
</div>
php code I am trying is
<?php echo \yii\bootstrap\Html::activeLabel($objAddPatientFrm, 'home_phn') ?>
<?php echo $objActiveForm->field($objAddPatientFrm, 'home_dial_code', [ 'inputOptions' => [ 'id' => 'dialCode']])->dropDownList($dialCodeArray)->label(false); ?>
<?php echo $objActiveForm->field($objAddPatientFrm, 'home_phn', [ 'inputOptions' => [ 'id' => 'home_phn']]); ?>
But the output is
<label for="addpatientform-home_phn">Home Phn</label> <div class="form-group field-dialCode required">
<select id="dialCode" class="form-control" name="AddPatientForm[home_dial_code]">
<option value="2">355,ALB</option>
<option value="3">213,DZA</option>
<option value="6">244,AGO</option>
<option value="224">971,ARE</option>
</select>
<p class="help-block help-block-error"></p>
</div>
<div class="form-group field-home_phn required">
<input type="text" id="home_phn" class="form-control" name="AddPatientForm[home_phn]">
<p class="help-block help-block-error"></p>
</div>
keeping both inputs and label in seperate form group .
Please suggest what can I do ?
Use following way to display forms:
// With 'default' layout you would use 'template' to size a specific field:
echo $form->field($model, 'demo', [
'template' => '{label} <div class="row"><div class="col-sm-4">{input}{error}{hint}</div></div>'
]);
// Input group
echo $form->field($model, 'demo', [
'inputTemplate' => '<div class="input-group"><span class="input-group-addon">#</span>{input}</div>',
]);
Hope it will help..
My solution to this is to prevent the $form->field() from rendering its own form-group class. This is achieved by using the options property and setting options.class to an empty string (or to include any classes other than form-group). Then wrap the fields in a <div class="form-group">.
Here's the code:
<div class="form-group">
<?= $form->field($Model, 'attribute1', ['options' => ['class' => '']]); ?>
<?= $form->field($Model, 'attribute2', ['options' => ['class' => '']]); ?>
</div>
For the OP's example:
<div class="form-group field-phn required">
<?php echo \yii\bootstrap\Html::activeLabel($objAddPatientFrm, 'home_phn') ?>
<?php echo $objActiveForm->field($objAddPatientFrm, 'home_dial_code', [ 'inputOptions' => [ 'id' => 'dialCode'], 'options' => ['class' => '']])->dropDownList($dialCodeArray)->label(false); ?>
<?php echo $objActiveForm->field($objAddPatientFrm, 'home_phn', [ 'inputOptions' => [ 'id' => 'home_phn'], 'options' => ['class' => '']]); ?>
</div>
I want to use dynamic form widget (wbraganca). I tried it using the tutorial by 'doingItEasy' channel & also by github. And wrote following code :
controller code -
public function actionCreate()
{
$model = new Vendors();
$modelsSubCat = [new BusinessSubCategories];
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$modelsSubCat = Model::createMultiple(BusinessSubCategories::classname());
Model::loadMultiple($modelsSubCat, Yii::$app->request->post());
// validate all models
$valid = $model->validate();
$valid = Model::validateMultiple($modelsSubCat) && $valid;
$modelsSubCat = Model::createMultiple(BusinessSubCategories::classname());
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
foreach ($modelsSubCat as $modelSubCat) {
$model->ven_sub_category_id = $modelSubCat->bsc_id;
if (! ($flag = $modelSubCat->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
return $this->redirect(['view', 'id' => $model->ven_id]);
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
} else {
return $this->render('create', [
'model' => $model,
'modelsSubCat' => (empty($modelsSubCat)) ? [new BusinessSubCategories] : $modelsSubCat
]);
}
}
'_form.php' code -
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use wbraganca\dynamicform\DynamicFormWidget;
?>
<div class="vendors-form">
<?php $form = ActiveForm::begin(['id' => 'dynamic-form']); ?>
<?= $form->field($model, 'ven_company_name')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'ven_main_category_id')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'ven_sub_category_id')->textInput() ?>
<div class="row">
<div class="panel panel-default">
<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' => 4, // 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' => $modelsSubCat[0],
'formId' => 'dynamic-form',
'formFields' => [
// 'bsc_id',
'bsc_name',
'bsc_image',
'bsc_description',
'bmc_id',
],
]); ?>
<div class="container-items"><!-- widgetContainer -->
<?php foreach ($modelsSubCat as $i => $modelSubCat): ?>
<div class="item panel panel-default"><!-- widgetBody -->
<div class="panel-heading">
<h3 class="panel-title pull-left">Sub Categories</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">
<?php
// necessary for update action.
if (! $modelSubCat->isNewRecord) {
echo Html::activeHiddenInput($modelSubCat, "[{$i}]id");
}
?>
<?= $form->field($modelSubCat, "[{$i}]bsc_name")->textInput(['maxlength' => true]) ?>
<div class="row">
<div class="col-sm-6">
<?= $form->field($modelSubCat, "[{$i}]bsc_image")->fileInput(); ?>
</div>
<div class="col-sm-6">
<?= $form->field($modelSubCat, "[{$i}]bsc_description")->textInput(['maxlength' => true]) ?>
</div>
</div><!-- .row -->
</div>
</div>
<?php endforeach; ?>
</div>
<?php DynamicFormWidget::end(); ?>
</div>
</div>
<?= $form->field($model, 'ven_services_offered')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'ven_business_logo')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'ven_company_descr')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'ven_established_date')->textInput() ?>
<?= $form->field($model, 'ven_noof_emp')->textInput() ?>
<?= $form->field($model, 'ven_branches_loc')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'ven_market_area')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'ven_website')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'ven_specialized_in')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'ven_contact_no')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'ven_email_id')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'ven_address')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'ven_country_id')->textInput() ?>
<?= $form->field($model, 'ven_state_id')->textInput() ?>
<?= $form->field($model, 'ven_city_id')->textInput() ?>
<?= $form->field($model, 'ven_location_id')->textInput() ?>
<?= $form->field($model, 'ven_zip')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'ven_contact_person_id')->textInput() ?>
<?= $form->field($model, 'ven_verified')->dropDownList([ 'Y' => 'Y', 'N' => 'N', ], ['prompt' => '']) ?>
<?= $form->field($model, 'ven_created')->textInput() ?>
<?= $form->field($model, 'ven_updated')->textInput() ?>
<?= $form->field($model, 'ven_deleted')->dropDownList([ 'Y' => 'Y', 'N' => 'N', ], ['prompt' => '']) ?>
<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(); ?>
</div>
<script type="text/javascript">
$(".dynamicform_wrapper").on("beforeInsert", function(e, item) {
console.log("beforeInsert");
});
$(".dynamicform_wrapper").on("afterInsert", function(e, item) {
console.log("afterInsert");
});
$(".dynamicform_wrapper").on("beforeDelete", function(e, item) {
if (! confirm("Are you sure you want to delete this item?")) {
return false;
}
return true;
});
$(".dynamicform_wrapper").on("afterDelete", function(e) {
console.log("Deleted item!");
});
$(".dynamicform_wrapper").on("limitReached", function(e, item) {
alert("Limit reached");
});
</script>
'create.php' code -
<?php
use yii\helpers\Html;
/* #var $this yii\web\View */
/* #var $model backend\models\Vendors */
$this->title = Yii::t('app', 'Create Vendors');
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Vendors'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="vendors-create">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'model' => $model,
'modelsSubCat' => $modelsSubCat
]) ?>
</div>
But What happens is that the add/remove button is not working. I'm showing it's screenshot - Screenshot with Add/remove buttons
1)first add
$form = ActiveForm::begin([
'options' => [
'enctype' => 'multipart/form-data',
'id' => 'dynamic-form'
]
]);
instead your
2) delete 'bmc_id' from attributes of dynamic form or add textinput to dynamic form with bmc_id column
3) check if exist your model Model (...Model::loadMultiple($modelsSubCat, Yii::$app->request->post());)
I tried, but i can't reproduce this issue here. Can you try fix the following lines and see if the problem was there?
You are not closing the <div class="row"> after the dynamic form.
This could mess up the html code.
In your formFields, you don't need to add the 'bmc_id' if the
model have one. Remove it. By the way, you are using:
Html::activeHiddenInput($modelSubCat, "[{$i}]id");
Make sure this is the correct name of the attribute.
Non-related with your issue, you have a second:
$modelsSubCat = Model::createMultiple(BusinessSubCategories::classname());
After the loadMultiple method, making it useless.
EDIT
Just occurred to me: by any chance you have more than one DynamicForm in the same view? Or the code is the same as you posted?