Hi there!

Currently making some test with a headless D8 and trying out the Entity Validation API.

I am using only the server side validation.

Based on the returned error, I need to map the error with the field having the error.

I have been playing with validation on title field, here is what I got:

When no data in title, I get this error:

Unprocessable Entity: validation failed. title: This value should not be null.

When adding constraint:

function mymodule_entity_bundle_field_info_alter(&$fields, \Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle) {
  if ($entity_type->id() == 'node' && $bundle == 'customer') {
    $fields['title']->addPropertyConstraints('value', ['Length' => ['min' => 3, 'max' => 10]]);
  }
}

I get this error:

Unprocessable Entity: validation failed. title.0.value: This value is too short. It should have 3 characters or more.

The field key are not the same. Is there any way to have the same structure? That would help a lot!

Also, is there a way to get the errors not formatted. Right now, I get a string, which I will have to manually split to extract the field ID and the error. It would be much simpler if I was able to get an array containing all errors, keyed by field ID for example.

Thanks!

Ed

Comments

edgab created an issue. See original summary.

Version: 8.5.0 » 8.5.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.

Version: 8.5.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Branches prior to 8.8.x are not supported, and Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
cilefen’s picture

Status: Active » Closed (outdated)

I am closing this support request because there have been no recent comments.

The Drupal Core issue queue is not the ideal place for support requests. Consider other sources of support.