首页 > 解决方案 > Yii2 掩码输入日期时间,dd-mm-yyyy hh:mm

问题描述

我使用Yii2 maskedInput将日期时间格式转换为表单。

<?php
    echo $form->field($modelIsoTanksDeliveryOrder, "open_stack", [
        'template' => '{input}{error}{hint}'
    ])
        ->widget(\yii\widgets\MaskedInput::className(), [
                'clientOptions' => [
                    'alias' => 'datetime',
                ],
                'options' => [
                    'placeholder' => '__-__-____ __:__'
                ]
            ]
        )->label(false)
?>

我正在尝试将“日期时间”掩码的行为更改为使用 dd-mm-yyyy hh:mm 而不是 dd/mm/yyyy hh:mm。

我在 JS RobinHerbots 上找到了配置但仍然失败

->widget(\yii\widgets\MaskedInput::className(), [
        'clientOptions' => [
            'alias' => 'dd-mm-yyyy',
            'separator' => "-",
        ],
        'mask' => '1-2-y h:s',
        'options' => [
            'placeholder' => '__-__-____ __:__'
        ]
    ]
)

标签: datedatetimeyii2

解决方案


您需要将 thealiasplaceholderandseparator选项一起用于以下clientOptions类似内容

<?php echo

    $form->field(
        $modelIsoTanksDeliveryOrder, "open_stack", [
            'template' => '{input}{error}{hint}'
        ]
    )->widget(
        \yii\widgets\MaskedInput::class, [
            'mask' => "1-2-y h:s",
            'clientOptions' => [
                'alias' => 'datetime',
                "placeholder" => "dd-mm-yyyy hh:mm",
                "separator" => "-"
            ]
        ]
    );

?>

在他的线程中查看更多详细信息


推荐阅读