Problem/Motivation

Spin-off from #1992138: Helper issue for "field types as TypedData Plugins" / #1969728: Implement Field API "field types" as TypedData Plugins.

That issue adds actual validation on entity fields.
This means that implicit or explicit constraints contained in FieldItem::getPropertyDefinitions() (e.g 'type' => 'uri' in LinkItem, 'type' => 'integer' for 'tid' in TaxonomyTermReferenceItem...) that were just "dormant" so far in HEAD, are now fired.

Issue

When failed, the 'type' constraints generate messages like : "this value should be of type 3" :-)
That's because internally the 'types' that are used by the PrimitiveTypeConstraintValidator are integer-based class constants in the Primitive class (Primitive::INTEGER is 3, Primitive::URI is 7...)

Also, this bug aside, those messages are not ideal UX-wise.
- We typically prefix the validation errors reported in forms with the name of the field:
"My custom link: this value should be..."
- The unique common template for those error messages makes awkward formulations:
"... should be of type integer / ... should be of type uri / ..."
We tend to generate more nicely put feedback: "this should be an integer, this should be a URL", but that's difficult to capture in one single fits-all message template.
I guess each primitive type could define its own error message ? Possibly even overridable in the propertyDefinition itself ?

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

fago’s picture

fago’s picture

There are labels for the primitives in the type definitions, we could read them from there but we need to add information on which type is the "default type" for a primitive.

effulgentsia’s picture

Discussed this with fago and yched in IRC. This will likely need us to create a DrupalExecutionContext that extends Symfony\Component\Validator\ExecutionContext, so that we can override addViolation() and friends to insert additional $params (e.g., name/title of field), that it could get from the Constraint instance definition.

fago suggested checking in to see if Symfony wants this capability upstream, but given our upcoming code freeze, I suggest doing whatever's fastest at this point. However, it probably makes sense to at least run a composer update on the Validator component, to make sure we're not coding against something already obsolete.

fago’s picture

Currently the Email field type has a dupcliated Length constraint in order to have a better message. So once we fix this we should remove that duplicated Length constraint there also - see https://drupal.org/node/2023563#comment-7943883.

fago’s picture

Issue summary: View changes

add structure

clemens.tolboom’s picture

This issue is referenced in core/modules/comment/src/Tests/CommentValidationTest.php:128 and I ran into this through #2220381: "This value should be of the correct primitive type" error when field has no value

    // @todo This message should be improved in
    //   https://www.drupal.org/node/2012690.
    $this->assertEqual($violations[0]->getMessage(), t('This value should be of the correct primitive type.'));

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

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

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

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

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

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

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

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

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

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

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

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

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

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

mlncn’s picture

Version: 8.6.x-dev » 8.8.x-dev
Status: Active » Needs review
StatusFileSize
new1.52 KB

This is surely not what we hope for as a proper patch, but it's a start, so people who find this issue can at least find where this terrible error message is being produced, and try to produce a better one at least for their own purposes.

Feedback on the language for this particular error message i'm suggesting—"String data cannot be an object or an array unless it is a markup object."—could help the final correct form of this issue also.

Status: Needs review » Needs work

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

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

ofrommel’s picture

I have also written a small patch (that failed some tests) https://www.drupal.org/project/drupal/issues/3056548. Your patch is probably better but you could probably also use $typed_data->getName() somewhere to deliver some more information.

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

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now 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: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Title: 'type' constraints generate crappy error messages. » Add useful information to 'type' constraints error messages
Issue summary: View changes
Issue tags: +Bug Smash Initiative, +Needs issue summary update, +Needs tests

Triaging for bugsmash.

@yched, Thanks for suggesting improving the error message.

This seems to be just about improving an error message. This will needs an Issue Summary update that explains the proposes resolution and a test. Adding tags for that. And steps to reproduce would help future reviewers. OH, and when there is a working patch screenshots before and after will be needed.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Title: Add useful information to 'type' constraints error messages » [Meta] Add useful information to 'type' constraints error messages
Issue tags: -Needs tests

Making this a meta to keep track of where the error messages need to be changed. And making #2925445: "this value should be of the correct primitive type" on empty integer fields a child issue.

clemens.tolboom’s picture

Added ref to issue mentioned by @ofrommel in #15

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.