In my project I have a repeatable select field. When I update a post, a new row is being added to the post. The value that is being set for the select in this new row is the first value in the options (not the default value).
On updating a post, just the content I created is being saved.
On updating a post, a new row is added to the repeatable select field. The value for this new row is the first one defined in the options, not the default value.
After updating a post, the bug occurs either when a row has been removed or when the field hasn’t been edited at all. The bug does not occur when a row has been added manually.
When I check the database I see that the automatically added rows are being saved in the database, so the bug goes beyond the interface.
This bug can be reproduced in multiple ways:
Update post.
Remove a row.
Update post.
Change the value for one of the select fields.
$cmb = new_cmb2_box( array(
'id' => $prefix . 'details',
'title' => 'Title',
'object_types' => array( 'training' ),
'context' => 'normal',
'priority' => 'default',
'show_names' => true,
) );
$cmb->add_field( array(
'name' => 'Name',
'desc' => 'Description',
'id' => $prefix . 'trainingstijden_trainingsdagen',
'type' => 'select',
'default' => 'negenacht',
'show_option_none' => false,
'options' => array(
'negeneen' => '09:00 - 13:00',
'negenvijf' => '09:00 - 17:00',
'negenacht' => '09:00 - 20:00',
'tweevijf' => '14:00 - 17:00',
),
'repeatable' => true,
) );
Can you confirm what version of CMB2 you are working with here, so that we can match the best we can.
I am using version 2.2.5.2.
Am able to see the behavior with version 2.2.5.2, for what it's worth.
The issue does go away with removing the repeatable, and I'm betting the behavior for repeatable fields, where it adds the UI for the next one for you, is causing the extra value to end up getting saved.
Possible, though understandably not ideal solution would be to move to a group of fields and the group is repeatable.
I've run into this before too. Because the nature of repeatable, there is always an additional set in the DOM. Since the select field is set to a default value and has the argument of show_option_none set to false, therefore the additional "hidden" set will have a value when saving the post. If you set show_option_none to true and remove the default, the first option in the select will then be an empty value thereby not being saved when the field is saved.
Good information @bnecreative, thanks for chiming in.
Thanks @bnecreative, that did the job for me!
Closing as logically expected behavior.
show_option_none to true and remove the default, the first option in the select will then be an empty value thereby not being saved
If I do this and have 3 values, and set the middle one to 'none', on next save it will be completely removed and I end up with only 2 values.
In my case I am forced to add an extra 'placeholder' option just so the entry doesn't get deleted entirely – but that's not very clear to the user (because there will be a 'None' option and my custom placeholder option – how would they know which to choose?).
Is this really expected behavior?
@rasco for your usecase, are you wanting to set a value that represents none? Say "no guacamole" for your fieldset, or are you by chance wanting a null/empty value in the middle of the available options?
If it's the first one, then setting an available option to be perhaps 'no_choice' => 'None' and whatnot would be the better route to take so that "no_choice" gets saved as meta that you can utilize. The "show option none" parameter is meant to not save data for that field if no other choice is selected.
@tw2113 Yeah it's kind of like a null/empty value in the middle of the array.
If I disable "show option none" I end up with a new field on every save (just like the original creator of this issue).
However, if I set "show option none" to true, I end up with two options that are misleading for the user:
One being "None" and the other being the custom placeholder I made for "null".
Hey @rasco I was looking at a different plugin that has a select with show_option_none utilized and noticed it used custom text.
That had me checking the CMB2 wiki and you can actually specify the value to use for that, instead of just setting it to true and falling to default value.
Set the optional paremeter, show_option_none, to true to use the default text, 'None', or specify another value, i.e. 'No selection'. By default show_option_none is false.
Not perhaps the perfect solution envisioned, but it would allow you to differentiate the two options better.
Most helpful comment
I've run into this before too. Because the nature of repeatable, there is always an additional set in the DOM. Since the select field is set to a default value and has the argument of show_option_none set to false, therefore the additional "hidden" set will have a value when saving the post. If you set show_option_none to true and remove the default, the first option in the select will then be an empty value thereby not being saved when the field is saved.