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

Does this package support fields with existing values? #48

Open
goldmerc opened this issue Nov 12, 2020 · 9 comments
Open

Does this package support fields with existing values? #48

goldmerc opened this issue Nov 12, 2020 · 9 comments
Labels
bug Something isn't working

Comments

@goldmerc
Copy link

Hi, just playing with this and I'm a bit confused.

Toggling field display depending on the value of another field is working perfectly but the fields which are being conditionally displayed are not being populated with their values from the database.

Not sure if I'm doing something wrong, or if this behavior is expected? Is the package designed to be used in a create scenario but not for updates?

Also, does it work on the detail view? Clearly not dynamically but for example, if 'exported' is set to true, display some extra fields: 'export_date', etc...

Thanks

@milewski
Copy link
Member

milewski commented Nov 13, 2020

It should work on all create/update/detail views, can you post some example of the code you wrote? perhaps you forgot to add the HasConditionalContainer to your resource

@goldmerc
Copy link
Author

Thanks. I have included the trait. I'm using Nova 3.15.0 & laravel 8.14.0

I've changed my code to illustrate the point but I'm testing with this code and still having the issue...

I have a panel with some fields that I only want the user to fill if the model is being exported. In my fields array...

new Panel('Export', $this->getExportFields())

    public function getExportFields()
    {
        return [
            Boolean::make('Export?', 'export'),

            Textarea::make('Description'),

            ConditionalContainer::make([
                Textarea::make('Description')
            ])->if('export truthy true'),
        ];
    }

The Textarea outside of the ConditionalContainer is being populated with its value from the database but the textarea inside the container is not. See screenshot.

Screenshot 2020-11-13 at 09 36 56

On the detail view, the second textarea is not displayed at all...

Screenshot 2020-11-13 at 09 42 45

Any suggestions?

@milewski
Copy link
Member

I think the issue is using the Panel. can you try without the panel to see if it works?

@milewski milewski added the bug Something isn't working label Nov 13, 2020
@goldmerc
Copy link
Author

I tried taking the fields out of the panel but it made no difference.

So, I played around a bit more and I think I worked out the problem. I have to apologise because I oversimplified the code above and hid the issue. I'm trying to use fields from a related resource, so the code should have been...

    public function getExportFields()
    {
        return [
            Boolean::make('Export?', 'affiliate->export'),

            Textarea::make('Description'),

            ConditionalContainer::make([
                Textarea::make('Description')
            ])->if('affiliate->export truthy true'),
        ];
    }

Having played around a bit more, I'm finding that if I use a field on the model to control the condition, the conditional fields do get populated. It was because I was using a related field 'affiliate->export' that it was breaking the field population.

@milewski
Copy link
Member

I see, yeah affiliate->export this wouldn't work.. the frontend tries to build a attributes map and match against array of data something like this:

$map = ['propertyA', 'propertyB', 'affiliate->export'];

$yourRealModelData = [ 'propertyA' => 1, 'propertyB' => 2,  'affiliate' => [  'export' => true ]  ];

so it cant find $yourRealModelData['affiliate->export'] that's they the item is always "hidden" because it always evaluates to false

@goldmerc
Copy link
Author

Understood. I've been trying to find a work around and getting some strange results. First I tried using a simple fillUsing / resolveUsing pair, like this...

Boolean::make('Export?', 'export')
    ->resolveUsing(function ($value, $resource, $attribute) {
        return $resource->affiliate->$attribute;
    })
    ->fillUsing(function ($request, $model, $attribute, $requestAttribute) {
        $model->affiliate->$attribute = $request[$requestAttribute];
    }),

ConditionalContainer::make($this->getConditionalFields())
    ->if('export truthy true'),

But the field values still didn't get populated. So, then I moved the logic into the model. In nova...

Boolean::make('Export?', 'export_to_affiliate')

ConditionalContainer::make($this->getConditionalFields())
    ->if('export_to_affiliate truthy true'),

In the model class...

public function getExportToAffiliateAttribute()
{
    return $this->affiliate->export;
}

public function setExportToAffiliateAttribute($export)
{
    $this->affiliate->export = $export;
}

This also didn't work. To be clear, the fields are being shown / hidden as expected but the field values are not being populated.

It's very strange because if I use a boolean value from the resource model as the condition, the conditional field values are populated correctly. So, the problem appears to be when the condition is from a related table, even when I try to camouflage that fact using the techniques above.

@folyjon
Copy link

folyjon commented Mar 17, 2021

Im having almost the same problem. In my case the ConditionalPanel fields do not not have values when in edit mode and are completely hidden when in view mode. Their conditon is based on a ComputedField (eg getIsActiveAttribute) in the Model

@adilbekes
Copy link

same problem(

@Akak-mx
Copy link

Akak-mx commented Jul 7, 2022

@folyjon Hey, I just started to use the package and find the same problem. Did you find a solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants