In submit use the entity built during the validation instead of building it again - for performance and consistency ensuring we are saving the entity that has been validated

Created on 7 December 2016, over 8 years ago
Updated 30 January 2023, over 2 years ago

Problem/Motivation

Currently in ContentEntityForm we build the entity once in the validation and once in the submit, however if the validation passed without errors then the submit will be called and will have to build the entity again.

This has two problems ;
1. In case of nested entity forms building the entity is expensive.
2. Because of entity builders doing changes to form state etc.. the entity build in validate and later in submit again might differ which means it might happen that we save an entity that we haven't validated and also might not be valid at all.

There is even an existing problem in core where we are saving an entity without completely validating it - πŸ› Currently we are saving an user entity with some not validated fields on the registration form Needs work and it just proves that it is so easy to oversee such things and how important this issue is in order to prevent saving invalid entities - in core, custom and contrib.

Proposed resolution

Store the entity build in the validation phase in $this->intermediateEntity and use this later in in submit instead of building the entity again.

Remaining tasks

Write the patch plus test ensuring we are saving the same entity that we have validated.

User interface changes

None.

API changes

None.

Data model changes

None.

πŸ“Œ Task
Status

Needs work

Version

10.1 ✨

Component
EntityΒ  β†’

Last updated about 3 hours ago

Created by

πŸ‡©πŸ‡ͺGermany hchonov πŸ‡ͺπŸ‡ΊπŸ‡©πŸ‡ͺπŸ‡§πŸ‡¬

Live updates comments and jobs are added and updated live.
  • Needs change record

    A change record needs to be drafted before an issue is committed. Note: Change records used to be called change notifications.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The Needs Review Queue Bot β†’ tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide β†’ to find step-by-step guides for working with issues.

Production build 0.71.5 2024