Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage of 'default()' callback on fields does not work #16

Open
garrettmassey1 opened this issue Nov 19, 2024 · 1 comment
Open

Usage of 'default()' callback on fields does not work #16

garrettmassey1 opened this issue Nov 19, 2024 · 1 comment

Comments

@garrettmassey1
Copy link

If your field uses the default() callback on any of the standard Nova fields, the callback either is not called or is not rendered properly because the resulting field on the view is not populated with any default values.

@garrettmassey1
Copy link
Author

I was able to get it to work by replacing the resolvePanel() method in the SettingsController with the following:

    private function resolvePanel(Panel $panel, $values, NovaRequest $request)
    {
        return collect($panel->data)
            ->each(function (Field $field) use ($values, $request) {
                $value = $values[$field->attribute] ?? null;

                // Resolve the value for the field
                $field->resolve($this->makeFakeResource($field->attribute, $value));

                // Apply default if no value exists
                if (is_null($value)) {
                    $field->resolve(
                        $this->makeFakeResource(
                            $field->attribute,
                            $field->resolveDefaultCallback($request)
                        )
                    );
                }
            })
            ->toArray();
    }

But I have not tested it beyond text fields and boolean fields / boolean group fields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant