i'm using Yii2 extension - Jui DatePicker.
Is there a way to show by default the current timestamp in the text box, so the user does not need to input it every time it fills de form fields?
My view, and the form in Yii2:
<?= $form->field($model, 'data')->widget(DatePicker::className(),
[
'language' => 'en',
'inline' => false,
'clientOptions' =>[
'dateFormat' => 'yyyy-MM-dd',
'showAnim'=>'fold',
'yearRange' => 'c-25:c+0',
'changeMonth'=> true,
'changeYear'=> true,
'autoSize'=>true,
'showOn'=> "button",
'buttonText' => 'clique aqui',
//'buttonImage'=> "images/calendar.gif",
]])
?>
In Yii1 i used to write the code like this (inside the widget), and the timestamp appeared by default:
'htmlOptions'=>array('size'=>12, 'value'=>CTimestamp::formatDate('d/m/Y')),
Is there an equivalent in Yii2?
Many thanks...
You can still use value:
<?= $form->field($model, 'data')->widget(DatePicker::className(),
[
'language' => 'en',
...
'value' => date('Y-m-d'),
As noted in a comment (haven't confirmed this)
'value' => date('Y-m-d') parameter will work only if widget is used without model: DataPicker::widget(['value' => date('Y-m-d')])
Therefore you can use clientOptions to set defaultDate :
'clientOptions' => ['defaultDate' => date('Y-m-d')]
Or better yet you can set $model->data to a default value of the current timestamp, either in your view or in the model.
if (!$model->data) $model->data = date('Y-m-d');
Related
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
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.
Please help... I am trying to set default value for this: I know it is based on Kartik select on yii2. I didn't use it before. Here is my source code I would need to set default value based on $_GET parameters. But problem is I can't set any. The source is this...
<?php
echo $form->field($profile, 'country_id')->widget(Select2::classname(), [
'language' => Yii::$app->language,
'data' => ArrayHelper::map($countries, 'id', 'title_' . mb_substr(Yii::$app->language, 0, 2)),
'theme' => Select2::THEME_BOOTSTRAP,
'options' => [
'id' => 'country-select',
'placeholder' => Yii::t('frontend', 'Select a country')],
'pluginOptions' => [
'allowClear' => true,
],
])->label($Country, ['class' => 'label-class'])
?>
Where Should I set it in this case. Sorry, I just saw this plugin at first time...
if you wanna set the default value, you should use the model like the following code;
$profile->country_id = isset($profile->country_id) ? $profile->country_id : 1 // like that
also you can use in afterFind function on the model.
check this Yii2: How to set default attribute values in ActiveRecord? and this https://www.yiiframework.com/doc/guide/2.0/en/tutorial-core-validators#default
My Jui Date Time Picker was working perfectly, I am not sure if I have made any changes in config anywhere, but now the date-picker field is showing the format as if it is asking for time with the date.
This is my code for the date field:
<?= $form->field($model, 'joining_date')->widget(DatePicker::className(),
['clientOptions' =>[
'dateFormat' => 'dd-mm-yyyy',
'showAnim'=>'fold',
'changeMonth'=> true,
'changeYear'=> true,
'autoSize'=>true,
'showOn'=> "button",
'buttonImage'=> "images/calendar.gif",
'htmlOptions'=>[
'style'=>'width:80px;',
'font-weight'=>'x-small',
],]]) ?>
in my Web.php config the relevant code for dates is like
'formatter' => [
'defaultTimeZone' => 'UTC',
'timeZone' => 'Asia/Kolkata',
'dateFormat' => 'php:d-m-Y',
'datetimeFormat'=>'php:d-M-Y H:i:s'
],
Now I am trying to enter the date I am getting the field like 12/07/2014:
and colon after the field as if I need to add the time part, where from it is coming, I couldn't find.
date-picker
Recently I ran into the same problem.
dateFormat is no longer the part of clientOptions and should be specified like that:
<?= $form->field($model, 'joining_date')->widget(DatePicker::className(), [
'dateFormat' => 'php:d-m-Y',
] ?>
Alternative in ICU format:
'dateFormat' => 'dd/MM/yyyy',
See official docs for $dateFormat property.
You may help this...
Yii Default date format is taken from configuration (thats the the power of YII::))
under component you have formate date....something how!!!
'formatter'=>[
'class'=>'yii\i18n\Formatter',
//'dateFormat' =>'MM/dd/yyyy',
'dateFormat' => 'php:d M Y',
'datetimeFormat' => 'php:d-M-Y H:i:s',
'timeFormat' => 'php:H:i:s',
],
use yii\jui\DatePicker;
<?php echo $form->field($ShipmentTypeForm, 'shipmendate')->widget(DatePicker::className(), [
'options' => ['class' => 'form-control input-sm','readOnly'=>'readOnly'],]); ?>
Alternatively use Bootstrap datepicker. You just need to include appropriate JS file and call something like this:
<?= $form->field($model, 'inputName', ['inputOptions'=>['class'=>'form-control datepicker']])->textInput() ?>
I am using datepicker in yii and I want to disable the previous dates on the calendar to avoid picking them.
Here's my code:
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model'=>$model,
'attribute'=>'arrival_date_as_per_recorded_travel',
'name'=>'arrival_date_as_per_recorded_travel',
'value'=>$eta_date_formatted,
// additional javascript options for the date picker plugin
'options'=>array(
'startDate'=>date("yy-mm-dd"),
'showAnim'=>'fold',
'dateFormat'=>'yy-mm-dd',
'changeMonth'=>'true',
'changeYear'=>'true',
'yearRange'=>'2013:2100',),
'htmlOptions'=>array(
'id'=>'arrival_date_as_per_recorded_travel',
'style'=>'height:20px;width:150px',
'value'=>$eta_date_formatted,
'onblur'=>'if(this.value=="")this.value=""'
),
));
As for Yii2 (added this as answer as the question is not tagged under yii 1.x)
<?php
use yii\jui\DatePicker;
?>
<?=
DatePicker::widget([
'name' => 'to_date',
'dateFormat' => 'dd/MM/yyyy',
'clientOptions' => [
'minDate' => 0
]
])
?>
change your options with the following code
'options'=>array(
'startDate'=>date("yy-mm-dd"),
'minDate'=>'0', // this will disable previous dates from datepicker
'showAnim'=>'fold',
'dateFormat'=>'yy-mm-dd',
'changeMonth'=>'true',
'changeYear'=>'true',
'yearRange'=>'2013:2100',),
'minDate'=>'0' will disable the previous dates...
You can take look of Jquery Date Picker API (minDate)
Hope it may help you...
I have tried previous answers but nothing worked.
Here is how i solved this
The minDate is not found in the documentations https://www.malot.fr/bootstrap-datetimepicker/
There is something like minDate no it's called startDate but it accepts only Date.
startDate Date. Default: Beginning of time The earliest date that may be selected; all earlier dates will be disabled. as in Offical documentations.
So here you go.
<?php
$now = new DateTime();
echo $form->field($data['model'], 'expire_date' ,
['template' =>
'{label}<p class="sub-label">' . \Yii::t("main","After this date people won't be able to bid on this job no more.").'</p> {input}{error}{hint}'])
->widget(DateTimePicker::classname(),
[
'options' => [
'placeholder' => \Yii::t("main","Enter a date..."),
'autoComplete' => 'off',
],
//'convertFormat' => true,
'language' => "Yii::$app->language;",
'pluginOptions' => [
'autoclose'=>true,
'format' => 'yyyy-mm-dd hh:ii:ss',
'startDate' => date_format($now, 'Y-m-d'), //startDate Date. Default: Beginning of time The earliest date that may be selected; all earlier dates will be disabled.
]
]);?>
Adding the following to your options array should help.
minDate: new Date(),