How to add UniqueField constraint on integer field? The title basically says it all. I have the following field definition in an Entity class:

$fields['code'] = BaseFieldDefinition::create('integer')
        -&gt;setLabel(t('unique code'))
        -&gt;setDescription(t('Some unique code'))
        -&gt;addConstraint('UniqueField')
        -&gt;setTranslatable(false)
        -&gt;setRequired(true)
        -&gt;setDisplayOptions('view', array(
            'label' =&gt; 'above',
            'weight' =&gt; 4,
        ))
        -&gt;setDisplayOptions('form', array(
            'weight' =&gt; 4,
        ))
        -&gt;setDisplayConfigurable('form', true)
        -&gt;setDisplayConfigurable('view', true);


Creating a new entity in the UI works as expected as long as I do not set the code value to something already in the database. However, as soon as I try to create a new entity with an already existing value for code (thus triggering a constraint violation), I get the following error:

The website encountered an unexpected error. Please try again later.
TypeError: Argument 1 passed to Drupal\Core\Form\FormState::setError() must be of the type array, null given, called in /var/www/html/web/core/lib/Drupal/Core/Field/WidgetBase.php on line 454 in Drupal\Core\Form\FormState-&gt;setError() (line 1156 of core/lib/Drupal/Core/Form/FormState.php).


If I change the field definition to use a string instead everything works as expected:

$fields['code'] = BaseFieldDefinition::create('string')
        -&gt;setLabel(t('unique code'))
        -&gt;setDescription(t('Some unique code'))
        -&gt;setSetting('max_length', 5)
        -&gt;addConstraint('UniqueField')
        -&gt;setTranslatable(false)
        -&gt;setRequired(true)
        -&gt;setDisplayOptions('view', array(
            'label' =&gt; 'above',
            'weight' =&gt; 4,
        ))
        -&gt;setDisplayOptions('form', array(
            'weight' =&gt; 4,
        ))
        -&gt;setDisplayConfigurable('form', true)
        -&gt;setDisplayConfigurable('view', true);


So the question is how do I add a unique constraint for an integer field? Is there another validation class I can use, am I setting up my integer field definition wrong thus causing the error, or?
This seems to be an open bug in core. I have filed a bug report containing a makeshift solution on Drupal.org: https://www.drupal.org/node/2784015/

Edit the file 'core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/NumberWidget.php' to let the errorElement function return $element[0]['value'] instead of $element['value']. So far, I don't understand why it is happening.