"this value should be of the correct primitive type" on empty integer fields

Created on 22 November 2017, almost 7 years ago
Updated 12 June 2024, 3 months ago

I have a custom entity type "enquiry" which has a custom field "product_enquiry" added to it.

On the edit form at /enquiry/123/edit I save and get "this value should be of the correct primitive type".

In the database "product_enquiries_quote_lead_time" is NULL, as expected - product enquiries are initially created without a lead time.

If I "UPDATE enquiry__product_enquiries SET product_enquiries_quote_lead_time = 0;", flush cache and then save the form again, the error no longer occurs. If I manually UPDATE the column back to NULL again the error comes back.

In PrimitiveTypeConstraintValidator I can see that $typed_data is an instance of IntegerData (as expected), and $value is an empty string. Surely $value should be undef in this case?

Currently it seems like it's impossible for a custom field to support an integer column that may be null.

Might be a duplicate of #2220381: "This value should be of the correct primitive type" error when field has no value β†’ but I'm not sure.

ProductEnquiryItem.php:

/**
* @FieldType(
*   id = "product_enquiry",
*...
* )
*/
class ProductEnquiryItem extends FieldItemBase implements FieldItemInterface {

    public static function schema(FieldStorageDefinitionInterface $field_definition) {
        return [
            'columns' => [
                ...
                'quote_lead_time' => [
                    'description' => 'The quoted lead time (in weeks).',
                    'type' => 'int',
                    'not null' => FALSE, // null until we set a lead time
                ],
            ],
            'indexes' => [
                ...
                'quote_lead_time' => [ 'quote_lead_time' ],
            ],
            'foreign keys' => [
                ...
            ],
        ];
    }

    public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
        $properties['quote_lead_time'] = DataDefinition::create('integer')->setLabel(t('Quoted lead time'));
        return $properties;
    }

DESCRIBE enquiry__product_enquiries:

+--------------------------------------+------------------+------+-----+---------+-------+
| Field                                | Type             | Null | Key | Default | Extra |
+--------------------------------------+------------------+------+-----+---------+-------+
| bundle                               | varchar(128)     | NO   | MUL |         |       |
| deleted                              | tinyint(4)       | NO   | PRI | 0       |       |
| entity_id                            | int(10) unsigned | NO   | PRI | NULL    |       |
| revision_id                          | int(10) unsigned | NO   | MUL | NULL    |       |
| langcode                             | varchar(32)      | NO   | PRI |         |       |
| delta                                | int(10) unsigned | NO   | PRI | NULL    |       |
...
| product_enquiries_quote_lead_time    | int(11)          | YES  | MUL | NULL    |       |
+--------------------------------------+------------------+------+-----+---------+-------+
πŸ› Bug report
Status

Active

Version

11.0 πŸ”₯

Component
Typed dataΒ  β†’

Last updated 2 days ago

  • Maintained by
  • πŸ‡¦πŸ‡ΉAustria @fago
Created by

πŸ‡¦πŸ‡ΊAustralia George Bills

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

    I'm also hitting the "This value should be of the correct primitive type" error with float configs, when I refactor the form to use the shiny new-ish #config_target functionality, and submit the form with blank/empty number form fields, which previously worked fine for submitting NULL values.

    I did add nullable: true to the config schema, but still get the same error when I submit the config form. I guess either PrimitiveTypeConstraintValidator should be made aware that the value is nullable and therefore empty string should be cast to NULL, or I need a way to disable/override this validator?

  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

    ...well what I ended up doing, to allow submitting empty string to a nullable float, was use a ConfigTarget callback to convert empty string to NULL (passing through any other string), and that seems to work fine.

  • πŸ‡ΊπŸ‡ΈUnited States EricB1021

    I haven't run into this on D11, but several times on D10.

    As a work around, in propertyDefinitions() create the property as $type = 'any'.
    e.g.
    $properties['item'] = DataDefinition::create('any')
    ->setLabel(t('Item Label'))
    ->setDescription(t('... Item description ...'));

    Then in schema() you can set your data type to what you need, instead of doing what mfb had to do.

Production build 0.71.5 2024