Updating a structure field deletes values

I try to update a structure field like this:

# /site/blueprints/files/colors.yml

...
fields:
  colors:
    type: structure
    fields:
      color: 
        type: text
      share: 
        type: number
<?php

$colors = [
    ['color' => 'red', 'share' => 0.25],
    ['color' => 'green', 'share' => 0.35],
    ['color' => 'blue', 'share' => 0.4],
];

$file->update(['colors' => $colors]);

In K4 this worked fine:

# file.png.txt

Colors:

- 
  color: "red"
  share: 0.25
- 
  color: "green"
  share: 0.35
- 
  color: "blue"
  share: 0.4

In K5 instead this results in the structure field containing three empty entries, ie. all data gets lost! Even converting the $colors array to Yaml explicitely does not help:

$file->update(['colors' => Yaml::encode($colors)]);
# file.png.txt

Colors:

- 
  color: null
  share: ""
- 
  color: null
  share: ""
- 
  color: null
  share: ""

Yes, i try to update a file’s meta data, not a page – but I assume, the issue affects pages as well.

I just tested this in a Starterkit for both a page and a file without issues. Which exact version of Kirby 5 are you using?

If you‘re using Kirby 5.0.0/5.0.1 you should update to 5.0.2, this issue is solved there:

Thanks to both of you @texnixe and @GB_DESIGN for your replies, each of it guided me on the right track :wink:

First: I am using the latest Kirby 5.0.2

Second: In my use case I have a CLI script that updates a bunch of images with some meta data while in the panel the users should find those data fields read only.

Third: I have to apologize that my code examples above were not precise enough: In fact, in the file blueprint the data fields inside the structure are configured to be disabled:

# /site/blueprints/files/colors.yml

...
fields:
  colors:
    type: structure
    fields:
      color: 
        type: text
        disabled: true
      share: 
        type: number
        disabled: true

And that turned out to be the problem: With disabled fields nested inside a structure their data is considered not to be submittable (thank you @GB_DESIGN for the hint) and therefore gets removed as soon as I try to update the structure (from CLI, see above) – might be a bit over the top, could be left as is instead, but it is as it is …

Meanwhile I found out that it helps to set the structure itself to be disabled and leave the nested fields enabled:

# /site/blueprints/files/colors.yml

...
fields:
  colors:
    type: structure
    fields:
      color:
        type: text
      share:
        type: number
    disabled: true

In this case I can update the structure as expected and in the panel the structure itself is read-only – perfect!

Great, but the question is why the disabled subfields could not be updated.

If I disable a standard field, I can update it unless I explicitly set the validate option to true.

Sounds a bit like a bug somewhere to me.