Yii 2 Editable, how to use editable inside form? - yii2

$form = ActiveForm::begin();
..
echo Editable::widget([ //this break outter form, because this generate another form
'name'=>'person_name',
'asPopover' => true,
'value' => 'Kartik Visweswaran',
'header' => 'Name',
'size'=>'md',
'options' => ['class'=>'form-control', 'placeholder'=>'Enter person name...']
]);
ActiveForm::end();
So, I tried,
echo Form::widget([
'model'=>$model,
'form'=>$form,
'columns'=>1,
'attributes'=>[
'title'=>[
'label'=>false,
'type' => Editable::INPUT_TEXT,
'widgetClass' => Editable::className(),
'options' => [
'asPopover' => true,
]
],
]
]);
but, it shows input box all the time, not editable text.
how can I use editable widget inside form? without breaking outter form?

You can try this way:
<?= $form->field($model, 'person_name')->Editable::widget([
'name'=>'person_name',
'asPopover' => true,
'value' => 'value',
'header' => 'Name',
'size'=>'md',
'options' => ['class'=>'form-control', 'placeholder'=>'Enter person name...']
]);
?>
Note : Not tested yet.

Related

How to reset kartik depdrop to initial state with initial data

i have a depdrop in my project like
<?= $form->field($model, 'neighborhood_id')->widget(Select2::classname(), [
'data' => ArrayHelper::map($query,'id','placeWithCity'),
'options' => ['placeholder' => 'Select Your Neighborhood','id'=>'select_place'],
])->label(false); ?>
And
<?=$form->field($model, 'building_id')->widget(DepDrop::classname(), [
'data' =>ArrayHelper::map(Buildings::find()->all(),'id','buildingWithPlace'),
'options' => ['placeholder' => 'Select The Building','id'=>'select_building'],
'type' => DepDrop::TYPE_SELECT2,
'pluginOptions' => [
'depends' => ['select_place'],
'url'=>Url::to(['property-commercial-rent/buildings']),
'loadingText' => 'Loading buildings ...',
]
])->label(false);?>
The second dropdown is depend on the first one.Initially they both have full list of data without any filtering.When i select a neighborhood in the first dropdown then the second populates with the building names under that neighborhood. Then i have a reset button like and has an action like
$( "#reset-location" ).click(function() {
$(select_place).val('').trigger('change');
$(select_building).val('').trigger('change');
});
And this click resetting the both select.BUt the problem is the building has only the items under the previously select neighborhood.I want the make it all like initial stage.How can i do this
You don't need a reset button. On the neighbourhood field allow clear, so it would be like this
<?= $form->field($model, 'neighborhood_id')->widget(Select2::classname(), [
'data' => ArrayHelper::map($query,'id','placeWithCity'),
'options' => ['placeholder' => 'Select Your Neighborhood','id'=>'select_place'],
'pluginOptions' => [
'allowClear' => true
],
])->label(false); ?>
Then there will be a x in the field to clear it.

How to add a placeholder to Select2 Widget - Yii2

I am working a yii2 page and it's giving me problems adding a placeholder or a prompt to Select2 Widget. Here is my code:
<?php
use yii\helpers\Html;
use kartik\widgets\ActiveForm;
use kartik\builder\Form;
use kartik\datecontrol\DateControl;
use yii\helpers\ArrayHelper;
/**
* #var yii\web\View $this
* #var app\models\FinancialAccounts $model
* #var yii\widgets\ActiveForm $form
*/
?>
<div class="financial-accounts-form">
<?php $form = ActiveForm::begin(['type' => ActiveForm::TYPE_VERTICAL]); echo Form::widget([
'model' => $model,
'form' => $form,
'columns' => 1,
'attributes' => [
'type_id' => ['type' => Form::INPUT_WIDGET, 'widgetClass'=>'\kartik\widgets\Select2', 'options' => ['data'=>ArrayHelper::map(app\models\FinancialAccountType::find()->all(), 'type_id', 'name')]],
'account_name' => ['type' => Form::INPUT_TEXT, 'options' => ['placeholder' => 'Enter Account Name...', 'maxlength' => 100]],
'account_code' => ['type' => Form::INPUT_TEXT, 'options' => ['placeholder' => 'Enter Account Code...', 'maxlength' => 10]],
'parent_id' => ['type' => Form::INPUT_WIDGET, 'widgetClass'=>'\kartik\widgets\Select2', 'options' => ['data'=>ArrayHelper::map(app\models\FinancialAccounts::find()->all(), 'account_id', 'account_name'), 'placeholder' => 'Select a Parent Account...']],
'description' => ['type' => Form::INPUT_TEXT, 'options' => ['placeholder' => 'Enter Description...', 'maxlength' => 250]],
],
]);
echo Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'),
['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']
);
ActiveForm::end(); ?>
</div>
The problem is in the parent_id attribute as I cannot add a placeholder as an option as most of the tutorials recommend. Everytime I try that, I get an error as this:
Unknown Property – yii\base\UnknownPropertyException
Setting unknown property: kartik\widgets\Select2::placeholder
Does anyone know how I can solve this? My main problem is that I cannot leave this option as blank when submitting data yet that is one of the possibility. It forces me to submit an item selected.
You'll note that if you follow the examples in the documentation carefully, placeholder needs to be wrapped within an options array.
'parent_id' => [
'type' => Form::INPUT_WIDGET,
'widgetClass' => '\kartik\widgets\Select2',
'options' => [
'data' => ArrayHelper::map(app\models\FinancialAccounts::find()
->all(), 'account_id', 'account_name'),
'options' => ['placeholder' => '...'],
'pluginOptions' => ['allowClear' => true],
]
],

Kartik datepicker extention not working on array input form

I have a form field "dob[]" with array input like
<?php $form = ActiveForm::begin();
for($i=0;$i<= 3;$i++):
echo $form->field($model, 'dob[]')->widget(DatePicker::classname(), [
'options' => ['placeholder' => 'Date Of Birth'],
'type' => DatePicker::TYPE_INPUT,
'pluginOptions' => [
'format' => 'mm/dd/yyyy',
'autoclose' => true,
]
]);
endfor; ?>
<div class="form-group">
<?= Html::button('Submit', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
Datepicker working only on first "dob" field but rest of the field having only button format of datepicker but calendar not working.
This is because the javascript cannot determine the correct input fields, after the first one. Take a look in your source code. All widgets have properly the same id and/or name. You have to setup a unique ID for each of the generated widgets.
By the way, it is always a good approach to name form data accordingly.
That is documented at the demo page.
The following should work:
<?php
$form = ActiveForm::begin();
for ($i=0; $i < 3; $i++) {
echo $form->field($model, 'date_end')->widget(DatePicker::classname(), [
'options' => [
'placeholder' => 'Date Of Birth',
'name' => 'DOB' .$i,
'id' => 'DOB-ID' . $i,
],
'type' => DatePicker::TYPE_INPUT,
'pluginOptions' => [
'format' => 'mm/dd/yyyy',
'autoclose' => true,
],
]);
}
?>
<div class="form-group">
<?= Html::button('Submit', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>

Remove encoding for popovers on labels

I'm using Yii2 to generate a popover on a label, but having some trouble to remove the default HTML encoding. I'm not sure that the popover can be created for just the label without HTML encoding and what the correct way to do this is, it must be possible though as Gii uses some variant of this code? This is what I've tried:
<?= $form->field($model, 'function')->textInput(['maxlength' => true])
->label(null, [
'class' => 'dashed-line',
'data-toggle' => 'popover',
'data-content' => 'This will be ran through <code>strtolower()</code>',
'data-placement' => 'right',
'encodeLabel'=> false]) ?>
Use
['labelOptions' => ['encode' => false]]
.
<?= $form->field($model, ['labelOptions' => ['encode' => false]] ,
'function')->textInput(['maxlength' => true])
->label(null, [
'class' => 'dashed-line',
'data-toggle' => 'popover',
'data-content' => 'This will be ran through <code>strtolower()</code>',
'data-placement' => 'right',
) ?>
you can use label option for setting the encode false of label attribute
<?= $form->field($model,
'function')->textInput(['maxlength' => true])
->label(null, [
'class' => 'dashed-line',
'data-toggle' => 'popover',
'data-content' => 'This will be ran through <code>strtolower()</code>',
'data-placement' => 'right',
'encode' => false,
) ?>

can use yii2 DepDrop::widget without model?

all developer kartik\DepDrop widget The ability to use without model
My code this but error
use kartik\widgets\DepDrop;
<?php echo DepDrop::widget([
'options' => ['id'=>'customer-city'],
'pluginOptions' => [
'depends => ['province'],
'placeholder => 'select ...',
'url' => Url::to(['/site/city'])
]
]); ?>
I use this code for show dropdown without lable
Yes, for it you need set the attribute name:
<?php echo DepDrop::widget([
'name' => 'city',
'options' => ['id'=>'customer-city'],
'pluginOptions' => [
'depends' => ['province'],
'placeholder' => 'select ...',
'url' => Url::to(['/site/city'])
]
]); ?>