Default Value for Balloon-Selector not applied if the field is added via addTabFields()

I just enlarge the UserModel with some new fields and face the problem, that the default values do not apply on balloon-selector fields.

I tested the following configuration in the October Test plugin (a modification of the status from the Location model):

    status:
        label: Status
        type: balloon-selector
        span: auto
        options:
            0: Active
            1: Draft
            2: Hidden
        trigger:
            action: enable
            field: is_enabled
            condition: checked
        default: 0

The default value is perfectly applied

Now this is how I modify my model:

        UserController::extendFormFields(function ($form, $model, $context) {
            if (!$model instanceof UserModel) return;
            $configFile = plugins_path('mch/general/models/user/fields.yaml');
            $config = Yaml::parse(File::get($configFile));
            $form->addTabFields($config['fields']);
        });

and this is my fields.yaml:

fields: 
    status:
        label: Status
        type: balloon-selector
        span: auto
        options:
            0: Active
            1: Draft
            2: Hidden
        trigger:
            action: enable
            field: is_enabled
            condition: checked
        default: 2

But the value is not applied here.

For me this looks like a bug, but maybe I overlooked something, so thx for any help :slight_smile:

Hey @LordRazen

Itā€™s working fine over here. Added to App\Provider

\RainLab\User\Controllers\Users::extendFormFields(function ($form, $model, $context) {
    if (!$model instanceof \RainLab\User\Models\User) return;
    $configFile = app_path('fields.yaml');
    $config = \Yaml::parse(\File::get($configFile));
    $form->addTabFields($config['fields']);
});

Creating a new user, the selector shows ā€œHiddenā€ selected by default.

Keep in mind, default values only apply under the following conditions:

protected function useDefaultValues(): bool
{
    return $this->context === FormField::CONTEXT_CREATE || $this->isNested || !$this->model->exists;
}
1 Like

Ok indeed I see the problem. It works on create.

Is there a way how I could overwrite this for the extendFormFields method somehow?

Iā€™m not sure where I could overwrite the useDefaultValues() method for my extension.

Yes.

In this event, setting the value on the field object should set its load value. Although, you may need to determine what ā€˜defaultā€™ state is.

$widget->getField('status')->value = true;