preselected values in DateControl yii 2 - php

I have the following code :
$form->field($model, 'start_time')->widget(DateControl::classname(), [
'type'=>DateControl::FORMAT_DATETIME,
'displayFormat' => 'php:d-M-Y H:i:s',
'ajaxConversion'=>true,
'options' => [
'pluginOptions' => [
'autoclose' => true
]
]
]);
that works perfectly. we can select the date and time and send the data to controller to save it.
The Problem
When I write this same code in update form, the values that are comming from the database does not show up in the datecontrol field. Its empty.. I have tried to provide a unix type datetime and a normal string(24-jul-2015) as well but it doesn't show any thing. Does any one know how to do that ?
other fields like following seems to be working perfectly and are being populated with the values from the database
<?= $form->field($model, 'price')->textInput() ?>

I found the answer by kartik.
just add
saveFormat=>'d-M-y'
under displayFormat or any other formate of your choice and it will display the saved value.. simple :)

Related

Using more than one widget in an attribute INPUT_WIDGET on kartik Form Builder

So I am doing a form using the kartik form builder and I am using the input_widget kartik\date\datepicker. When I use the datepicker only it submits but when I go to the item view the date is set like 30/11/-0001. The solution I found when I first had this problem was using not only the date picker but also the date control (also from kartik). But at that time I was using the default yii form and it was possible to use multiple widgets in a single attribute. Now im using the kartik form builder and i cant set more than one widget and its class. How can I do to use both date picker and date control in a single attribute at kartik's form build or, if anyone knows, how to solve this problem without having to add the date control?
before it was like:
<?= $form->field($model, 'validadeCertificado')
->textInput()
->widget(DatePicker::classname(), [
'pluginOptions' => [
'autoclose'=>true,
]
])
->widget(DateControl::className(), [
'type'=>DateControl::FORMAT_DATE,
'ajaxConversion'=>false,
'widgetOptions' => [
'pluginOptions' => [
'autoclose' => true,
'language' => 'pt-BR',
]
]
]);
?>
now it is like:
echo Form::widget([ // 3 column layout
'model'=>$model,
'form'=>$form,
'columns'=>12,
'compactGrid'=>true,
'attributes'=>[
'validadeCertificado'=>[
'type'=>Form::INPUT_WIDGET,
'widgetClass'=>'\kartik\date\DatePicker',
'options' => [
'pluginOptions' => [
'format' => 'dd-mm-yyyy',
'autoclose'=>true,
'todayHighlight' => true
],
],
'columnOptions'=>['colspan'=>3],
],
<!--(other attributes)-->
]
]);```
I just wanted the date to be correctly inserted in the mysql database but it is inserted into the database as 0000-00-00
Heeeelp

How to display 2 months with datepicker yii2

I need to display 2 months using datepicker here
<?= $form->field($model, 'date_debut')->widget(\yii\jui\DatePicker::class, [
'language' => 'fr',
'dateFormat' => 'yyyy-MM-dd',
'options' => ['class' => 'form-control']
]) ?>
What you need is two inputs. The first one will be for the start date and the second one will be for the end date. If you happen to need to open both inputs at once when you click on one of them, you should trigger the event on the other one. I.e. start_date.onclick should trigger end_date.onclick and the vice versa.

Kartik Select 2 - Programatically changing multiple

I have a yii2 activeform where the functionality of the form can change based on other things within the form. So, I have a clubs field, that can be multiple in some instances but not multiple in others.
<?= $form->field($model, 'clubs')->widget(\kartik\widgets\Select2::classname(), [
'data' => $club_data,
'hideSearch' => false,
'options' => ['placeholder' => 'Add Club(s)'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
'minimumInputLength' => 3,
'ajax' => [
'url' => 'web/index.php?r=clubs/clubslist',
'dataType' => 'json',
'data' => new JsExpression('function(params) { return {q:params.term}; }'),
],
],
])->label('Club(s)'); ?>
I need to programmatically change the multiple pluginOption to true and false. This should be when the user goes into the form but also immediately when a dropdown is changed on the form. I can do it when the user initially goes into the form but not immediately when another dropdown is changed.
I've made separate fields, one actually linked to a field in the database and another not, this kind of works but it's far from elegant!
I've looked in both the kartik select2 documentation and the standard jquery select2 documentation and I can't spot anything. Any help or pointers would be much appreciated.
Reference
I was working on a Yii2-formwizard plugin a few months back where I had a situation of providing tabular functionality and cloning the fields really became the pain in the ass when it came to using 3rd party plugins like Select2 and Datepicker and I figured out the following code along with a lot of other (as the other part isn't relevant to your problem so not adding it).
Approach to the problem
The way Kartik-select2 works it stores the select2 plugin-specific options in the data-krajee-select2 attribute of the element you are using the select2 on.
And the theme or say the Kartik's plugin-specific options like theme and others are saved in the variable whose name is saved in the data-s2-options attribute. You can look for both of them in view-source of the page where you are using it.
Solution
So what you need to do is to
Steps
Get the applied options
Add/Update the option
Apply back the options
As you just added a single field and not the other one on who's selection it would change, I would demonstrate an example where you can change the behavior of the select2 from multiple to single using 2 different buttons, clicking on the relevant button the select2 will work accordingly. You can adjust the javascript code where ever you want to.
But before I add any code you need to fix one thing, you don't need the main data option when you are using the ajax option inside the plugin options. In other words, it does not have any effect on your select2 and is adding page load time only you should remove it.
You select2 should look like below, I added id to the options of the select2 for the example you can change it to the active form formatted name like model-field_name format if you like, but don't forget to change the id in the javascript code too
<?php echo $form->field($model, 'clubs')->widget(\kartik\widgets\Select2::classname(), [
'hideSearch' => false,
'options' => ['placeholder' => 'Add Club(s)','id'=>'clubs'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
'minimumInputLength' => 3,
'ajax' => [
'url' => 'web/index.php?r=clubs/clubslist',
'dataType' => 'json',
'data' => new JsExpression('function(params) { return {q:params.term}; }')
]
]
])->label('Club(s)');?>
Add the following buttons on your page
echo Html::button('multiple', ['class' => 'btn btn-info', 'id' => 'multi']);
echo Html::button('single', ['class' => 'btn btn-info', 'id' => 'single']);
And now the magic thing, add it on the top of your view
$js = <<<JS
var element=$("#clubs");
$('#multi').on('click',function(e){
//reset select2 values if previously selected
element.val(null).trigger('change');
//get plugin options
let dataSelect = eval(element.data('krajee-select2'));
//get kartik-select2 options
let krajeeOptions = element.data('s2-options');
//add your options
dataSelect.multiple=true;
//apply select2 options and load select2 again
$.when(element.select2(dataSelect)).done(initS2Loading("clubs", krajeeOptions));
});
$('#single').on('click',function(e){
element.val(null).trigger('change');
let dataSelect = eval(element.data('krajee-select2'));
let krajeeOptions = element.data('s2-options');
dataSelect.multiple=false;
$.when(element.select2(dataSelect)).done(initS2Loading("clubs", krajeeOptions));
});
JS;
$this->registerJs($js, \yii\web\View::POS_READY);
Now if your select2 is working correctly click on the multiple button and you will see the multiple selections are enabled for the select2 and when clicked on the single button.
Hope it helps.

Yii2 kartik-datecontrol timepicker Update

I am using a DateControl widget by Kartik in my Yii2-powered system. The widget correctly saves the time I've selected. However, when I tried to update the data, it just always shows "12:30" as the time and not the time from the database. I am still new to Yii2 and I there's not much information on the Internet regarding this issue. Thank you for the help!
Code for my form:
<?= $form->field($model, 'class_start_time')->widget(DateControl::classname(), [
'type'=>DateControl::FORMAT_TIME,
])
?>
<?= $form->field($model, 'class_end_time')->widget(DateControl::classname(), [
'type'=>DateControl::FORMAT_TIME,
])
?>
Code for the config:
'displaySettings' => [
Module::FORMAT_DATE => 'dd-MM-yyyy',
Module::FORMAT_TIME => 'HH:mm a',
Module::FORMAT_DATETIME => 'dd-MM-yyyy HH:mm:ss a',
],
// format settings for saving each date attribute (PHP format example)
'saveSettings' => [
Module::FORMAT_DATE => 'php:U', // saves as unix timestamp
Module::FORMAT_TIME => 'php:H:i:s',
Module::FORMAT_DATETIME => 'php:Y-m-d H:i:s',
],
I found the solution. The displaySettings should be hh:mm a and not HH:mm a. There is a mismatch in the format which causes the display to be in error when the time is in PM (or greater than 12:00:00).

yii2 datapicker set endDate property from model

I am using yii2 datepicker plugin and I had set the start date as today's date.
Now, I want to set endDate from my database. It varies as per record cancel date, which is different for each record:
<?= $form->field($model, 'cancel_date')->widget(DatePicker::classname(), [
'pluginOptions' => [
'autoclose'=>true,
'format' => 'dd/mm/yyyy',
'startDate' => (string)date('d/m/Y'),
//'endDate' => '1/1/2017'
],
])->label(false);
?>
I need to set that endDate property to the value from my database.
You can render Your popup body with ajax. In controller action, responsible for rendering modal body, You can find corresponding model, and then pass it to popup view. In view just use $model->cancel_date as end_date param.

Categories