3

The title basically says it all. I have the following field definition in an Entity class:

$fields['code'] = BaseFieldDefinition::create('integer')
        ->setLabel(t('unique code'))
        ->setDescription(t('Some unique code'))
        ->addConstraint('UniqueField')
        ->setTranslatable(false)
        ->setRequired(true)
        ->setDisplayOptions('view', array(
            'label' => 'above',
            'weight' => 4,
        ))
        ->setDisplayOptions('form', array(
            'weight' => 4,
        ))
        ->setDisplayConfigurable('form', true)
        ->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->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')
        ->setLabel(t('unique code'))
        ->setDescription(t('Some unique code'))
        ->setSetting('max_length', 5)
        ->addConstraint('UniqueField')
        ->setTranslatable(false)
        ->setRequired(true)
        ->setDisplayOptions('view', array(
            'label' => 'above',
            'weight' => 4,
        ))
        ->setDisplayOptions('form', array(
            'weight' => 4,
        ))
        ->setDisplayConfigurable('form', true)
        ->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?

2
  • 1
    Maybe see drupal.org/node/2478663
    – user21641
    Commented Jan 28, 2016 at 15:34
  • I can confirm that the NumberWidget currently throws Recoverable fatal error: Argument 1 passed to Drupal\Core\Form\FormState::setError() must be of the type array, null given. I am on Drupal v8.1.7
    – Gogowitsch
    Commented Aug 14, 2016 at 18:29

2 Answers 2

0

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.

0

It might be a core bug, I believe it should be fairly easy to overcome. The addConstraint method takes a second parameter with options:

public DataDefinition::addConstraint($constraint_name, $options = NULL)

This defaults to NULL, but according to the documentation can also be an array. Your error indicates that an array is expected. So you should be able to use this instead:

->addConstraint('UniqueField', [])
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.