0

I have a custom entity with an integer field with the following baseFieldDefinition:

    $fields['permission_level'] = BaseFieldDefinition::create('integer')
    ->setLabel(t("Permission Level"))
    ->setDescription(t("Sets amount of permissions owner has"))
     ->addConstraint('Range', ['min' => 0])
    ->setDefaultValue(0)
    ->setDisplayOptions('form', [
      'type' => 'number'
    ]);

because of the ->addConstraint('Range', ['min' => 0]), drupal throws an error anytime I try to create an entity through either the drupal generate:entity:content generated boilerplate form or json:api post requests.

I have used Xdebug to narrow the issue to the constraint validator range. Something is passing a FieldListItem object instead of an integer to this constraint validator causing it to fail because its not an integer. The weird thing is that inside the FieldListItem object is the integer I input.

Why is drupal passing a FieldListItem object instead of an Integer?

0

You can add constraints to entities, fields or properties. You get then an entity, field object (FieldItemList) or a value to validate. So if you want to check the field value use FieldConfigInterface::addPropertyConstraints:

->addPropertyConstraints('value', ['Range'=> ['min' => 0]])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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