cannot set serialize=0 via api3 when creating custom fields
Overview
When I create a custom field via the api v3, and I set serialize=0, I get an error: "'0' is not a valid option for field serialize"
I discovered this problem because of an extension that creates and updates existing custom fields. It first gets the existing field definition, then makes changes, and then tries to update it. But, if the existing field has serialize=0
, then the update fails.
Reproduction steps
- Setup the test CiviCRM data.
- Run the following command:
cv api CustomField.create custom_group_id=1 label=Test data_type=String html_type=Radio serialize=0
Current behaviour
www-data@51ea4a2af0c6:~/civicrm/civicrm-buildkit/build/mycivi/web$ cv api CustomField.create custom_group_id=1 label=Test data_type=String html_type=Radio serialize=0
{
"error_field": "serialize",
"error_code": 2001,
"entity": "CustomField",
"action": "create",
"is_error": 1,
"error_message": "'0' is not a valid option for field serialize"
}
www-data@51ea4a2af0c6:~/civicrm/civicrm-buildkit/build/mycivi/web$
Note: I can leave off serialize=0 and it works fine.
Expected behaviour
I would expect the command to complete without error.
Comments
I can see a few different avenues for a fix and am happy to create a PR for the one that seems the best.
I think this is the best - but not sure if it would have any negative consequences:
- We could modify the allowed values for serialize in CRM_Core_SelectValues::fieldSerialization so that 0 is allowed.
Some easier and hackier options:
- Work around in my own code. We could say api v3 is not being actively maintained and we should move to apiv4 (or I could wrap a fix into my code to drop the serialize line if it's set to 0).
- We could continue in this hacky tradition of adding an exception.
Or, maybe there is a better solution? Thanks for any ideas!