Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't work with kartik DateControl and DatePicker widget #41

Open
Murik opened this issue May 21, 2015 · 9 comments
Open

Doesn't work with kartik DateControl and DatePicker widget #41

Murik opened this issue May 21, 2015 · 9 comments
Labels

Comments

@Murik
Copy link

Murik commented May 21, 2015

https://github.com/kartik-v/yii2-datecontrol

<?php DynamicFormWidget::begin([

...
Add

....

$interval): ?>
                                <?= $form->field($interval, "[{$i}]start_date")->widget(
                                    DateControl::className(),
                                    ['type' => DateControl::FORMAT_DATE,
                                        'ajaxConversion' => false,
                                    ]); ?>

.....




After dynamic add DateControl and set date indicates an error that field is not filled.
possible to find a solution?

@wbraganca wbraganca changed the title Doesn't work with kartik DateControl widget Doesn't work with kartik DateControl and DatePicker widget May 26, 2015
@cloudcaptain
Copy link

looking forward to this bug fix

@HenryDewa
Copy link

looking to this bug fix

@cloudcaptain
Copy link

The following fix is working for me.

        // "kartik-v/yii2-widget-datecontrol"
        var $hasDateControl = $(this).find('[data-krajee-datecontrol]');
        if ($hasDateControl.length > 0) {
            $hasDateControl.each(function() {
                var id = $(this).attr('id');
                var dcElementOptions = eval($(this).attr('data-krajee-datecontrol'));
                if (id.indexOf(dcElementOptions.idSave) < 0) {
                    // initialize the NEW DateControl element
                    var cdNewOptions = $.extend(true, {}, dcElementOptions);
                    cdNewOptions.idSave = $(this).next().attr('id');
                    $(this).kvDatepicker(eval($(this).attr('data-krajee-kvdatepicker')));
                    $(this).removeAttr('value name data-krajee-datecontrol');
                    $(this).datecontrol(cdNewOptions);

                }
            });
        }

@Discomania
Copy link

@cloudcaptain
your fix is not working for me. I had to edit your code. this is my modified code

        // "kartik-v/yii2-widget-datecontrol"
        var $hasDateControl = $(widgetOptionsRoot.widgetItem).find('[data-krajee-datecontrol]');
        if ($hasDateControl.length > 0) {
            $hasDateControl.each(function() {
                var id = $(this).attr('id');
                var dcElementOptions = eval($(this).attr('data-krajee-datecontrol'));
                if (id.indexOf(dcElementOptions.idSave) < 0) {
                    // initialize the NEW DateControl element
                    var cdNewOptions = $.extend(true, {}, dcElementOptions);
                    cdNewOptions.idSave = $(this).parent().next().attr('id');
                    $(this).parent().datepicker(eval($(this).attr('data-krajee-datepicker')));
                    $(this).removeAttr('value name data-krajee-datecontrol');
                    $(this).datecontrol(cdNewOptions);

                }
            });
        }

I hope it is helpful to someone and that this bug will be corrected in yii2-dynamic-form.js

@fabioaccetta
Copy link

Talking about only DatePicker widget, it seem that Kartik has changed attribute names, so code in yii2-dynamic-form.js#L351:

[...]

var $hasDatepicker = $(widgetOptionsRoot.widgetItem).find('[data-krajee-datepicker]');
if ($hasDatepicker.length > 0) {
   $hasDatepicker.each(function() {
      $(this).parent().removeData().datepicker('remove');
      $(this).parent().datepicker(eval($(this).attr('data-krajee-datepicker')));
   });
}

[...]

can't work anymore, because for example data-krajee-datepicker is now called data-krajee-kvdatepicker, and the datepicker() function itself is now called kvDatepicker()...

This way, someone else has already tried to solve the fix here; so I have tried to use the same fix and what happens in my case is this: the widget appears correctly, but the parsing of php format does not.

This is my partial:

echo DatePicker::widget([
    'form' => $form,
    'language' => Yii::$app->language,
    'type' => DatePicker::TYPE_COMPONENT_PREPEND,
    'model' => $model,
    'attribute' => "[{$i}]birthday",
    'convertFormat' => true,
    'pluginOptions' => [
        'endDate' => '+0d',
        'autoclose' => true,
        'format' => 'php:Y-m-d',
        'todayHighlight' => true,
    ],
]);

and when I click on a date on the calendar, year and day are correct, but month is not; for example, I click on 2015-07-31 and the field value becomes: 2015-ii-31.

I hope my informations can help someone to solve the problem. Thanks

@fabioaccetta
Copy link

News: the date parsing error comes from Kartik exentions when DatePicker widget is called that way (because php date parsing is called twice, going to error), so changing to this:

$picker = [
    'language' => Yii::$app->language,
    'type' => DatePicker::TYPE_COMPONENT_PREPEND,
    'convertFormat' => true,
    'pluginOptions' => [
        'endDate' => '+0d',
        'autoclose' => true,
        'format' => 'php:Y-m-d',
        'todayHighlight' => true,
    ],
];
echo $form->field($model, "[{$i}]birthday")->widget(DatePicker::className(), $picker);

php parsing now works as espected.
Moreover, I have verified that patching yii2-dynamic-form.js with new Kartik attributes and functions - '[data-krajee-datepicker]' to '[data-krajee-kvdatepicker]' and datepicker() to kvDatepicker() - everything seems to work as espected.

@mrsad
Copy link

mrsad commented Aug 11, 2015

@Discomania great!

@wbraganca, i confirm, this code is working.

patch:

--- yii2-dynamic-form.js.old    2015-08-11 23:27:48.273959445 +0800
+++ yii2-dynamic-form.js        2015-08-11 21:39:13.000000000 +0800
@@ -309,12 +309,30 @@
     var _restoreSpecialJs = function(widgetOptions) {
         var widgetOptionsRoot = _getWidgetOptionsRoot(widgetOptions);

+        // "kartik-v/yii2-widget-datecontrol"
+        var $hasDateControl = $(widgetOptionsRoot.widgetItem).find('[data-krajee-datecontrol]');
+        if ($hasDateControl.length > 0) {
+            $hasDateControl.each(function() {
+                var id = $(this).attr('id');
+                var dcElementOptions = eval($(this).attr('data-krajee-datecontrol'));
+                if (id.indexOf(dcElementOptions.idSave) < 0) {
+                    // initialize the NEW DateControl element
+                    var cdNewOptions = $.extend(true, {}, dcElementOptions);
+                    cdNewOptions.idSave = $(this).parent().next().attr('id');
+                    $(this).parent().kvDatepicker(eval($(this).attr('data-krajee-kvdatepicker')));
+                    $(this).removeAttr('value name data-krajee-datecontrol');
+                    $(this).datecontrol(cdNewOptions);
+
+                }
+            });
+        }
+
         // "kartik-v/yii2-widget-datepicker"
-        var $hasDatepicker = $(widgetOptionsRoot.widgetItem).find('[data-krajee-datepicker]');
+        var $hasDatepicker = $(widgetOptionsRoot.widgetItem).find('[data-krajee-kvdatepicker]');
         if ($hasDatepicker.length > 0) {
             $hasDatepicker.each(function() {
-                $(this).parent().removeData().datepicker('remove');
-                $(this).parent().datepicker(eval($(this).attr('data-krajee-datepicker')));
+                $(this).parent().removeData().kvDatepicker('remove');
+                $(this).parent().kvDatepicker(eval($(this).attr('data-krajee-kvdatepicker')));
             });
         }

rizasaputra pushed a commit to rizasaputra/yii2-dynamicform that referenced this issue Mar 15, 2016
@PeterJason
Copy link

Anyone have a solution for this problem? My datepicker doesnt work (after add new item), i'm using kartik\date\DatePicker, i made the changes, but the problem continues..

Anyone have another solution?
Thank you

@ajkosh
Copy link

ajkosh commented Apr 8, 2017

here is working solution for date picker and datetimepicker kartik 's widget

 var $hasDateControl = $(widgetOptionsRoot.widgetItem).find('[data-krajee-datecontrol]');
        if ($hasDateControl.length > 0) {
           $hasDateControl.each(function() {
                var id = $(this).attr('id');
                var dcElementOptions = eval($(this).attr('data-krajee-datecontrol'));
                if (id.indexOf(dcElementOptions.idSave) < 0) {
                    // initialize the NEW DateControl element
                    var cdNewOptions = $.extend(true, {}, dcElementOptions);
                    $types=cdNewOptions.type;
                    if($types=='datetime'){
                       $(this).parent().datetimepicker(eval($(this).attr('data-krajee-datetimepicker')));  
                    }else{
                        $(this).parent().kvDatepicker(eval($(this).attr('data-krajee-kvdatepicker')));
                    }
                    cdNewOptions.idSave = $(this).parent().next().attr('id');
                    $(this).removeAttr('value name data-krajee-datecontrol');
                    $(this).datecontrol(cdNewOptions);
                }
           });
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants