When saving a form with the Office Hours fields that contains a value of "0300" (which represents 12:30 AM), the error "This value should be of the correct primitive type." appears for the start time of "00:30".

The module currently defines the starthours and endhours fields as integers, as follows:

  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $properties['day'] = DataDefinition::create('integer')
      ->setLabel(t('Day'))
      ->setDescription("Stores the day of the week's numeric representation (0-6)");
    $properties['starthours'] = DataDefinition::create('integer')
      ->setLabel(t('Start hours'))
      ->setDescription("Stores the start hours value");
    $properties['endhours'] = DataDefinition::create('integer')
      ->setLabel(t('End hours'))
      ->setDescription("Stores the end hours value");

    return $properties;
  }

The custom Office Hours validation first runs, followed by the Drupal core validations. Within Drupal's core validations, the PrimitiveTypeConstraintValidator validation fails because a time of "0300", which represents 12:30 AM, is not an integer, but rather a string.
To fix, the data definition for the time fields need to be defined as strings so they are validated as such by Drupal core. The required change is as follows:

    $properties['starthours'] = DataDefinition::create('string')
...
    $properties['endhours'] = DataDefinition::create('string')
...

This change does not change how the office hour start and end times are calculated and stored in the database.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

sbreese created an issue. See original summary.

sbreese’s picture

casey’s picture

FileSize
1.34 KB

Or we could just cast the form inputs to ints during massageFormValues().

johnv’s picture

Status: Active » Needs review
johnv’s picture

Component: Code » Code - widget
johnv’s picture

Title: Getting "This value should be of the correct primitive type." error for start time of "00:30" » Error "This value should be of the correct primitive type." for start time of "00:30"
Issue summary: View changes

  • johnv committed ec204a6 on 8.x-1.x authored by casey
    Issue #2850486 by sbreese, casey, johnv: Error "This value should be of...
johnv’s picture

Assigned: sbreese » Unassigned
Status: Needs review » Fixed

I committed a fix using casey's approach. Part of the code went into OfficeHoursWidgetBase:massageFormValues(), because the error appease both on both available widgets ('Week' and 'List').

Thanks.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.