I want to show dates which are greater than current date
in ValidFrom and dates greater than ValidFrom in ValidUpto field but unable to show.I want hide previous dates in date picker.
My code is below.
echo DatePicker::widget([
'model' => $model,
'attribute' => 'ValidFrom',
'attribute2' => 'ValidUpto',
'options' => ['placeholder' => 'valid from',
],
'options2' => ['placeholder' => 'valid to'],
'type' => DatePicker::TYPE_RANGE,
'form' => $form,
'pluginOptions' => [
'format' => 'yyyy-mm-dd',
'autoclose' => true,
],
]);
Add a id attribute to the date picker:
echo DatePicker::widget([
'id'=>'mydatepicker',
'model' => $model,
'attribute' => 'ValidFrom',
'attribute2' => 'ValidUpto',
'options' => ['placeholder' => 'valid from',
],
'options2' => ['placeholder' => 'valid to'],
'type' => DatePicker::TYPE_RANGE,
'form' => $form,
'pluginOptions' => [
'format' => 'yyyy-mm-dd',
'autoclose' => true,
],
]);
Set minDate to the date picker in your view file:
<?
$script = <<< JS
$( "#mydatepicker").datepicker({dateFormat: "yyyy-mm-dd", minDate: 0});
JS;
$this->registerJs($script, View::POS_END);
?>
Related
How to hide history dropdown list in Yii2 kartik select2?
This is my source code:
<div class="col-sm-offset-2 col-sm-10" style="margin-left:135px;margin-bottom:15px">
<label class="control-label">Range Perjalanan Dinas</label>
<?= DatePicker::widget([
'type' => DatePicker::TYPE_RANGE,
'model' => $model,
'attribute' => 'tgl_mulai',
'attribute2' => 'tgl_selesai',
'options' => ['placeholder' => 'Start date'],
'options2' => ['placeholder' => 'End date'],
'form' => $form,
'pluginOptions' => [
'format' => 'dd-mm-yyyy',
'autoclose' => true,
]
]); ?>
</div>
Try 'autocomplete' => 'off' in your date picker options:
<?= DatePicker::widget([
'type' => DatePicker::TYPE_RANGE,
'model' => $model,
'attribute' => 'tgl_mulai',
'attribute2' => 'tgl_selesai',
'options' => ['placeholder' => 'Start date','autocomplete' => 'off'],
'options2' => ['placeholder' => 'End date'],
'form' => $form,
'pluginOptions' => [
'format' => 'dd-mm-yyyy',
'autoclose' => true,
]
]); ?>
You may have to turn off the autocomplete in your form element
Turning off form autocomplete
In my model I use DateValidation
['date_birthday', 'date', 'format' => 'd.m.yy', 'min' => '01.01.1900', 'max' => date('d.m.yy'), 'tooSmall'=>'The date is from past. Try another','tooBig' => 'The date is from future. Try another', 'message' => 'Try to input the date'],
In view I call the widget
<?php echo $form->field($modelForm, 'date_birthday')->widget(\kartik\date\DatePicker::classname(), [
'type' => \kartik\date\DatePicker::TYPE_COMPONENT_APPEND,
'pickerButton' => false,
'options' => [
'placeholder' => '',
],
'pluginOptions' => [
'format' => 'dd.mm.yyyy',
'autoclose' => true,
'showMeridian' => true,
'startView' => 2,
'minView' => 2,
]
]) ?>
It checks for min and max dates, but show no error message. I think its because of different date formats in model and view. How to fix it?
If you submit form you will see error messages. According to this issue https://github.com/yiisoft/yii2/issues/7745 yii2 have not client-side validations of date
You can enable ajax validation. Add in create and update action before if statement
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = yii\web\Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
And add use yii\widgets\ActiveForm; on the top of controller class. In your _form.php file enable ajax for whole form
<?php $form = ActiveForm::begin([
'enableAjaxValidation' => true,
]); ?>
, or for the field only
<?php echo $form->field($model, 'date_birthday', ['enableAjaxValidation' => true])->widget(\kartik\date\DatePicker::classname(), [
...
Also, you can add plugin options for limit date with startDate and endDate (https://bootstrap-datepicker.readthedocs.io/en/latest/options.html#startdate)
<?php echo $form->field($model, 'date_birthday', ['enableAjaxValidation' => true])->widget(\kartik\date\DatePicker::classname(), [
'type' => \kartik\date\DatePicker::TYPE_COMPONENT_APPEND,
'pickerButton' => false,
'options' => [
'placeholder' => '',
],
'pluginOptions' => [
'format' => 'dd.mm.yyyy',
'autoclose' => true,
'showMeridian' => true,
'startView' => 2,
'minView' => 2,
'startDate' => '01.01.1900',
'endDate' => date('d.m.Y'),
]
]) ?>
Below is the snapshot of my index page in Yii2 with Kartik Gridview widget;
Below is the gridview code for the same.
<?= GridView::widget([
'dataProvider' => $dataProvider,
//'filterModel' => $searchModel,
'columns' => [
[
'class' => 'kartik\grid\SerialColumn',
'contentOptions' => ['class' => 'kartik-sheet-style'],
'headerOptions' => ['class' => 'kartik-sheet-style'],
],
//'line_id',
'metal_name',
'item',
'unit_name',
'weight',
['attribute' => 'quantity',
'pageSummary' =>(true),
'value' => function ($model) {
if($model){
return $model->quantity;
}
}],
'rate_per_gram',
'making_charges',
[
'attribute' => 'total',
'format'=>'currency',
'pageSummary' =>(true),
'value' => function ($model) {
if($model){
return $model->total;
}
}
],
['class' => 'kartik\grid\ActionColumn'],
],
'responsive'=>true,
'hover'=>true,
'export'=>false,
'showPageSummary' => true,
]); ?>
I have tried lot of stuff to get plain number format with 2 decimals, but i could succeed in one.
How can I Format my Total column with Indian Currency, viz. Rs. 35670.00 or just 35670.00?
One way is to set currency directly:
[
'attribute' => 'total',
'format' => ['currency', 'INR'],
'pageSummary' => true,
],
PS. You don't need to set value key unless you want to replace it with something else in certain case.
I have two date fields, the maximum date cannot be before the initial date, so I disabled the pickupMaxDate, and enabled it again in onChange(). Right there I need to set the startDate param to the value selected in the first DatePicker
This is what i've tried:
<?=
$form->field($shipment, 'pickupDate')->widget(
DatePicker::className(), [
'clientOptions' => [
'autoclose' => true,
'todayHighlight' => true,
'language' => 'es',
'startDate' => '0d',
'format' => 'dd-M-yyyy'
],
'clientEvents' => [
'change' => 'function (dateText, inst) {
//-> WORKING
$("#shipment-pickupdatemax").prop("disabled", false);
//-> NOT WORKING
var mindate = $(this).datepicker("getDate"); //-> PRINTS (Wed Sep 28 2016 00:00:00 GMT-0600 (CST))
$("#shipment-pickupdatemax").datepicker("option", "startDate", mindate);
}',
],
]);
?>
<?=
$form->field($shipment, 'pickupDateMax')->widget(
DatePicker::className(), [
'clientOptions' => [
'autoclose' => true,
'todayHighlight' => true,
'language' => 'es',
'startDate' => '0d',
'format' => 'dd-M-yyyy'
],
'options' => [
'disabled' => true
]
]);
?>
How do I achieve this?
I am using kartik datepicker extension http://demos.krajee.com/widget-details/datepicker in Yii2.
Issue :
If I use this with custom id for input it does not show show model validations
echo kartik\date\DatePicker::widget([
'model' => $objPatientModel,
'form'=>$objActiveForm,
'attribute' => 'date_of_birth',
'options' => ['placeholder' => 'Enter birth date ...', 'id' => 'patient_dob'], **// with id clientside validations for model does not work**
'pluginOptions' => [
'format' => 'dd-mm-yyyy',
'endDate' => date('d-m-Y'),
]
]);
While below code works
echo kartik\date\DatePicker::widget([
'model' => $objPatientModel,
'form'=>$objActiveForm,
'attribute' => 'date_of_birth',
'options' => ['placeholder' => 'Enter birth date ...'],**//id is not used here**
'pluginOptions' => [
'format' => 'dd-mm-yyyy',
'endDate' => date('d-m-Y'),
]
]);
But I want to use custom id .Any suggestions ?
You have to override the selectors, example:
$form->field($model, 'comment', ['selectors' => ['input' => '#myCustomId']])
->textarea(['id' => 'myCustomId']);?>
See https://github.com/yiisoft/yii2/issues/7627
<?=
$form->field($model, 'sales_date')->widget(DateControl::classname(), [
'name' => 'sales_date',
'value' => date('d-m-Y h:i:s'),
'type' => DateControl::FORMAT_DATETIME,
'autoWidget' => true,
'displayFormat' => 'php:d-m-Y h:i:s',
'saveFormat' => 'php:Y-m-d h:i:s',
'saveOptions' => [
'type' => 'hidden',
'form' => 'sales-form-red',
],
])
?>
by using the following option you can add extra form tag to input
'saveOptions' => [
'type' => 'hidden',
'form' => 'sales-form-red',
'class' => 'sales_date',
]
In my case out was
<input type="hidden" id="sales-sales_date" name="Sales[sales_date]" form="sales-form-red">
check by using link
Date Control Demo