yii 2 date picker only allow before today - php

echo $form->field($model, 'dob')->widget(DatePicker::classname(), [
'options' => [
'placeholder' => Yii::t('app', 'Please pick your birth-date'),
],
'type' => DatePicker::TYPE_INPUT,
'pluginOptions' => [
'autoclose'=>true,
'format' => 'dd/mm/yyyy',
'startView' => 1,
],
])->label(Yii::t('app', 'Date of Birth'));
The code above is used to create a DatePicker in yii 2. Is it possible for me to only allow users to select date before today?

Yes, you can the future dates disable by endDate property of pluginOptions.
echo $form->field($model, 'dob')->widget(DatePicker::classname(), [
'options' => [
'placeholder' => Yii::t('app', 'Please pick your birth-date'),
],
'type' => DatePicker::TYPE_INPUT,
'pluginOptions' => [
'autoclose'=>true,
'format' => 'dd/mm/yyyy',
'startView' => 1,
'endDate'=>date('d/m/Y')
],
])->label(Yii::t('app', 'Date of Birth'));

Related

Yii2 kartik datetime picker retain value after page submitting

I am using yii2-widget-datetimepicker in my yii2 project. I want to retain the value of it after submitting the page.
<form action="index" method="post" >
<?=
DateTimePicker::widget([
'name' => 'datetime_10',
'options' => [
'placeholder' => 'Start Date Time',
'autocomplete' => 'off',
'required' =>true,
],
'convertFormat' => false,
'pluginOptions' => [
'format' => 'yyyy-mm-dd hh:i:ss',
//'startDate' => '01-Mar-2014 12:00 AM',
'todayHighlight' => true,
'autoclose' => true,
]
]);
?>
<?=
DateTimePicker::widget([
'name' => 'datetime_11',
'options' => [
'placeholder' => 'End Date Time',
'autocomplete' => 'off',
'required' =>true,
],
'convertFormat' => false,
'pluginOptions' => [
'format' => 'yyyy-mm-dd hh:i:ss',
//'startDate' => '01-Mar-2014 12:00 AM',
'todayHighlight' => true,
'autoclose' => true,
]
]);
?>
</form>
How to retain the selected date-time value? Any help would be highly appreciated.
Without further tweaks, you could do
<form action="index" method="post">
<?=
DateTimePicker::widget([
'name' => 'datetime_10',
'value' => Yii::$app->request->post('datetime_10', null),
'options' => [
'placeholder' => 'Start Date Time',
'autocomplete' => 'off',
'required' => true,
],
'convertFormat' => false,
'pluginOptions' => [
'format' => 'yyyy-mm-dd hh:i:ss',
//'startDate' => '01-Mar-2014 12:00 AM',
'todayHighlight' => true,
'autoclose' => true,
]
]);
?>
<?=
DateTimePicker::widget([
'name' => 'datetime_11',
'value' => Yii::$app->request->post('datetime_11', null),
'options' => [
'placeholder' => 'End Date Time',
'autocomplete' => 'off',
'required' => true,
],
'convertFormat' => false,
'pluginOptions' => [
'format' => 'yyyy-mm-dd hh:i:ss',
//'startDate' => '01-Mar-2014 12:00 AM',
'todayHighlight' => true,
'autoclose' => true,
]
]);
?>
</form>
But this is not reliable. If you are gathering data throughout multiple pages, you should really consider storage/persistence, at least session & save to database after all the forms are completed, or skip session and go directly with in-memory data, a database etc.
You should assigne a value realted to your nodel->field and assigne/reassigne the value you need after (or before ) subimit
<?=
DateTimePicker::widget([
'name' => 'datetime_10',
'value' => $yourModel->yourField, // <------ this
'options' => [
'placeholder' => 'Start Date Time',
'autocomplete' => 'off',
'required' =>true,
],
'convertFormat' => false,
'pluginOptions' => [
'format' => 'yyyy-mm-dd hh:i:ss',
//'startDate' => '01-Mar-2014 12:00 AM',
'todayHighlight' => true,
'autoclose' => true,
]
]);
?>

How to use onSelect event in Kartik datePicker?

I am using kartik datePicker in my site but whenever I try to use onSelect event the doesn't triggered I don't know what is the problem. I searched a lot but didn't find any solution.
This is my code:
<?php
echo DatePicker::widget([
'name' => 'check_issue_date',
'type' => DatePicker::TYPE_INLINE,
'options' => ['placeholder' => 'Select issue date ...', 'id' => 'date-picker'],
'pluginOptions' => [
'format' => 'dd-M-yyyy',
'todayHighlight' => false,
'onSelect' => 'function(dateText, inst) {
alert("hello");
}'
]
]);
?>
Look at pluginEvents on this page http://demos.krajee.com/widget-details/datepicker#settings
<?php echo DatePicker::widget([
'id'=>'scheduleProject',
'name' => 'scheduleProject',
'type' => DatePicker::TYPE_INLINE,
'language' => 'en',
'pluginOptions' => [
'format' => 'M-dd-yyyy',
'startDate' => date(\Yii::$app->params['viewDateFormat']),
'todayHighlight' => true,
'todayBtn' => true,
],
'pluginEvents' =>[
"changeDate" => "function(e) { alert(123)}",
]
]);?>
echo DatePicker::widget([
'name' => 'check_issue_date',
'type' => DatePicker::TYPE_INLINE,
'options' => ['placeholder' => 'Select issue date ...', 'id' => 'date-picker'],
'pluginOptions' => [
'format' => 'dd-M-yyyy',
'todayHighlight' => false,
'onSelect' => 'function(dateText, inst) {
alert("hello");
}'
],
'pluginEvents' => [
'onChange' => 'function(event) {
console.log(event.date);
}'
]
]);
?>

I have 2 columns referenced from another table A, and I want to display usernames as they are saved in in table B

I know how to display name instead of id in Yii framework in gridview and detail view.
Here is part of codes in view.php
<?= DetailView::widget([
'model' => $model,
'condensed' => false,
'hover' => true,
'attributes' => [
'user_branch_id',
//'user_id',
[
'attribute'=>'user_id',
'value' =>$model->user->username,
],
//'branch_id',
[
'attribute'=>'branch_id',
'value' =>$model->branch->branch_name,
],
'status',
/* [
'attribute' => 'created_at',
'format' => [
'datetime', (isset(Yii::$app->modules['datecontrol']['displaySettings']['datetime']))
? Yii::$app->modules['datecontrol']['displaySettings']['datetime']
: 'd-m-Y H:i:s A'
],
'type' => DetailView::INPUT_WIDGET,
'widgetOptions' => [
'class' => DateControl::classname(),
'type' => DateControl::FORMAT_DATETIME
]
],
*/
**// 'created_by',
[
'attribute'=>'created_by',
'value' =>$model->user->username,
],**
[
'attribute' => 'last_update',
'format' => [
'datetime', (isset(Yii::$app->modules['datecontrol']['displaySettings']['datetime']))
? Yii::$app->modules['datecontrol']['displaySettings']['datetime']
: 'd-m-Y H:i:s A'
],
'type' => DetailView::INPUT_WIDGET,
'widgetOptions' => [
'class' => DateControl::classname(),
'type' => DateControl::FORMAT_DATETIME
]
],
],
'deleteOptions' => [
'url' => ['delete', 'id' => $model->user_id],
],
'enableEditMode' => true,
]) ?>
I have these attributes:
1. [
'attribute'=>'user_id',
'value' =>$model->user->username,
],
2. [
'attribute'=>'created_by',
'value' =>$model->user->username,
],
when I retrieve as shown on a screenshot below, it show same username, while id are different i.e.
column created_by: 1
column user_id: 3
Finally I solve my problem, at the model, because two columns are foreign keys from same table user, the model generate two get functions
getUser()
getCreatedBy
Then my code solutions are
[
'attribute'=>'user_id',
'value' =>$model->user->username,
],
'attribute'=>'created_by',
'value' =>$model->createdBy->username,
],

yii2 grid filter type not working

I am using kartik grid lib in yii2
This works.
[
'attribute' => 'status',
'filterType' => GridView::FILTER_SELECT2,
'filter' => ['Active' => 'Active', 'In Active' => 'In Active'],
'filterWidgetOptions' => [
'pluginOptions' => ['allowClear' => true],
],
'filterInputOptions' => ['placeholder' => 'Select'],
]
But this doesn't
[
'attribute' => 'status',
'filterType' => '\kartik\widgets\Select2',
'filter' =>['Active' => 'Active', 'In Active' => 'In Active'],
'filterWidgetOptions' => [
'pluginOptions' => ['allowClear' => true],
],
'filterInputOptions' => ['placeholder' => 'Select'],
]
Why ? I need to use the point 2 approach. But it is not working.
Changed filter type to '\kartik\select2\Select2' and it worked.
usa 'filterType' =>GridView::FILTER_SELECT2, el composer usado es use kartik\grid\GridView;
'filterType' => GridView::FILTER_SELECT2,
'filter' => ArrayHelper::map(Entidad::find()->asArray()->all(),'id', 'nombre'),
filterWidgetOptions' => [
'pluginOptions' => ['allowClear' => true],
],
'filterInputOptions' => ['placeholder' => 'Selección'],

Yii2: kartik/DateTimePicker prevent selection of a date before today

I have tried with below code and it is not working for me.
echo $form->field($model, 'value')->widget(DateTimePicker::classname(), [
'pluginOptions' => [
'autoclose' => true,
'format' => 'yyyy-mm-dd HH:ii:ss',
'minuteStep' => 1,
'todayHighlight' => true,
'startDate' => date("y-m-d H:i:s"),
'changeYear' => true,
'changeMonth' => true,
]
])->label('Select date & time');
It works for me as below:
$form->field($model, 'value')->widget(DateTimePicker::classname(), [
'options' => ['placeholder' => 'Enter Value'],
'pluginOptions' => [
'autoclose' => true,
'format' => 'Y-m-d H:i:s',
'startDate' => date('Y-m-d H:i:s')
]
]);
Add 'minDate' => 0 to disable previous date selection from today.
Updated Code
echo $form->field($model, 'value')->widget(DateTimePicker::classname(), [
'pluginOptions' => [
'autoclose' => true,
'format' => 'yyyy-mm-dd HH:ii:ss',
'minuteStep' => 1,
'minDate' => 0,
'todayHighlight' => true,
'startDate' => date("y-m-d H:i:s"),
'changeYear' => true,
'changeMonth' => true,
]
])->label('Select date & time');
echo $form->field($model, 'value')->widget(DateTimePicker::classname(), [
'pluginOptions' => [
'minuteStep' => 1,
'minDate' => 0,
'startDate' => date("y-m-d H:i:s"),
]
])
You can use the following i assume you are using kartik\widgets\DateTimePicker
$form->field($model, 'campaign_schedule', [
'inputOptions' =>
[
'value' => date('Y-m-d H:i:s'),
],
])->widget(kartik\widgets\DateTimePicker::class, [
'options' => ['placeholder' => 'Select operating time ...'],
'convertFormat' => false,
'pluginOptions' => [
'format' => 'yyyy-mm-dd hh:ii:ss',
'todayHighlight' => true,
'startDate' => new JsExpression("new Date('" . date('m/d/y') . "')"),
'autoclose' => true,
],
])

Categories