[英]Yii2 include datetimepicker in ActiveForm
我正在使用Activeform,並且需要一個自定義字段來插入日期時間值。
我自己用kartik DateTimePicker找到了可能的解決方案,盡管它比我想要的復雜得多,但無論如何我都不知道如何將值放入模型的屬性中。
use kartik\datetime\DateTimePicker;
<?php $form = ActiveForm::begin(); ?>
// my working field using kartik library
<?= $form->field($model, 'course')->widget(\kartik\select2\Select2::className(),[
'data' => $data,
'language' => 'it',
'options' => ['placeholder' => 'Select a Course ...'],
'pluginOptions' => [
'allowClear' => true
],
]);?>
// the field I need to implement placing the result in $form->field($model, 'opening_date')
<?php //echo '<label style="position:absolute; top:5px; text-align:left;">Recording Time</label>';
echo '<label>Start Date/Time</label>';
echo DateTimePicker::widget([
'name' => 'opening_date',
'options' => ['placeholder' => 'Select operating time ...'],
'convertFormat' => true,
'pluginOptions' => [
'format' => 'd-M-Y g:i A',
'startDate' => '01-Mar-2014 12:00 AM',
'todayHighlight' => true
]
]);
?>
我也接受提供更簡單的方式來顯示該領域的解決方案。
如果我正確理解的話,您是在詢問將模型與小部件一起使用,而不是靜態地調用它,因為您當前正在將值復制到另一個ActiveForm字段中,然后提交它。 如果正確,則可以將小部件用作ActiveForm
字段的一部分,這樣就不ActiveForm
日期插入到單獨的模型輸入中即可提交和保存,請參見下文。
<?php echo $form->field($model, 'opening_date')->widget(
DateTimePicker::class,
[
'options' => ['placeholder' => 'Select operating time ...'],
'convertFormat' => true,
'pluginOptions' => [
'format' => 'd-M-Y g:i A',
'startDate' => '01-Mar-2014 12:00 AM',
'todayHighlight' => true
]
]
);
?>
或在調用如下所示的小部件時,可以將$model
和attribute
傳遞給當前代碼
echo DateTimePicker::widget(
[
'model' => $model,
'attribute' => 'opening_date',
'options' => ['placeholder' => 'Select operating time ...'],
'convertFormat' => true,
'pluginOptions' => [
'format' => 'd-M-Y g:i A',
'startDate' => '01-Mar-2014 12:00 AM',
'todayHighlight' => true
]
]
);
您可以通過name
/ model
和attribute
選項查看詳細信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.