Winter CMS resources and help articles

Simple and to the point. Optimized by the community.

Dynamically change the DatePicker formWidget mode.

0
by mjauvin, last modified on May 13th, 2025

If you have datepicker field in your form, as shown below:

fields:
    startDate:
        label: Start Date
        type: datepicker
        mode: datetime
        dependsOn: mode

    dateMode:
        type: dropdown
        options:
            date:  Date Only Mode
            datetime: Date And Time Mode

And you need to change it dynamically based on the dateMode field for example, do this in your Plugins.php file:

public function boot()
{
    YourModel::extend(function ($model) {
        $model->bindEvent('model.form.filterFields', function ($form, $fields, $context) {
            if (isset($fields->dateMode)) {
                $mode = $fields->dateMode->value;
                $form->getFormWidget('startDate')->mode = $mode;
            }
        }); 
    });
}

Discussion

0 comments

We use cookies to measure the performance of this website. Do you want to accept these cookies?