Open goldmerc opened 3 years ago
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
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.
On the detail view, the second textarea is not displayed at all...
Any suggestions?
I think the issue is using the Panel. can you try without the panel to see if it works?
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.
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
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.
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
same problem(
@folyjon Hey, I just started to use the package and find the same problem. Did you find a solution?
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