Skip to content

Recommend using form types as data mappers in the docs #11395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2019

Conversation

wouterj
Copy link
Member

@wouterj wouterj commented Apr 10, 2019

Fixes #11335

Turns out we did already document implementing the interface in the form type, but only in a small caution at the end. I've swapped it, so we're now using the stateless way in the docs and a small sidebar quickly shows creating a separate data mapper.

/cc @mpdude @webmozart

Copy link
Member

@javiereguiluz javiereguiluz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice changes! Thanks.

@javiereguiluz javiereguiluz added this to the 3.4 milestone Apr 10, 2019
@wouterj wouterj force-pushed the issue-11335/data-mapper branch from ab80083 to f5b6871 Compare April 10, 2019 14:33
@wouterj
Copy link
Member Author

wouterj commented Apr 10, 2019

Thanks for the review. I've fixed your comments

@javiereguiluz
Copy link
Member

Thank you Wouter.

@javiereguiluz javiereguiluz merged commit f5b6871 into symfony:3.4 Apr 11, 2019
javiereguiluz added a commit that referenced this pull request Apr 11, 2019
…wouterj)

This PR was merged into the 3.4 branch.

Discussion
----------

Recommend using form types as data mappers in the docs

Fixes #11335

Turns out we did already document implementing the interface in the form type, but only in a small caution at the end. I've swapped it, so we're now using the stateless way in the docs and a small sidebar quickly shows creating a separate data mapper.

/cc @mpdude @webmozart

Commits
-------

f5b6871 Recommend using form types as data mappers in the docs
.. tip::
.. sidebar:: Stateful Data Mappers

Sometimes, data mappers need to access services or need to maintain their
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes, data mappers need to access services

This is actually wrong. When a data mapper needs a service, you can still implement DataMapperInterface in FormType and add the service to the constructor of the form type.

Indeed the only legit reason for creating a separate data mapper class if you want to use form options in the data mapper.

$form = $this->createForm(SomeType::class, $data, ['option_for_data_mapper' => 123]);

Since option values are specific to Form instances and not to the single FormType instance, we need multiple data mappers with different states here. Otherwise you can implement DataMapperInterface in FormType.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thanks for clarifying. I was already wondering about the same thing.

@wouterj wouterj deleted the issue-11335/data-mapper branch April 11, 2019 08:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants