Skip to content

Add FormInterface::getChangedFieldsList #59456

Open
@michnovka

Description

@michnovka

Description

I'd like to propose a simple way to extract the list of all entity properties that were changed upon form submission. Similar to how Doctrine computes the changeset during the flush operation, but tailored for forms mapped to entities.

Use Case:

When a form is mapped to an entity, Symfony goes through each field and updates the entity properties accordingly. However, currently, there's no straightforward way to retrieve a list of which properties were actually modified during the form submission.

Having access to this list would be incredibly useful for several reasons:

  1. Granular Permission Checks: In scenarios where different user roles have permissions to edit only certain fields, being able to identify which fields are being modified allows us to enforce these permissions more effectively.

  2. Audit Logging: For applications that require detailed logging of changes (e.g., admin action logs), knowing exactly which properties were changed enables us to create more informative and accurate logs.

  3. Optimized Updates: By knowing the changed fields ahead of time, we can optimize database operations, perhaps updating only the necessary fields.

Current Workarounds:

While it's possible to clone the entity before form submission and manually compare the properties after submission, this approach has its drawbacks:

  • It's cumbersome and adds boilerplate code to controllers.
  • Deep cloning entities can be problematic, especially with complex associations.
  • It doesn't leverage the fact that the form component already processes each field.

Proposed Solution:

Introduce a method or mechanism within the Symfony form handling process that provides a list of changed properties after form submission. Perhaps something like:

$changedFields = $form->getChangedFields();

or even accessing it via the data mapper:

$changedFields = $form->getDataMapper()->getChangedFields();

Example

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy