[Form] Allow specifying CollectionType
's "prototype_name" as callable
#57231
+11
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Nesting
CollectionType
s which utilize prototypes requires diligently definingprototype_name
manually to avoid collisions of the default value (__name__
). This has been a pain point since I started using prototypes and I've long wished to be able to dynamically specify the placeholder. I added thecallable
type to the allowed types forprototype_name
and call it once at the beginning ofbuildForm()
to resolve the actual (string) value, and then store that in aFormConfig
resolved_prototype_name
attribute in case it needs to be accessed later.In my application code I extend
CollectionType
(viagetExtendedTypes()
), set the defaultprototype_name
option value to a function that generates a placeholder name containing random hex characters, and inbuildView()
inject that placeholder (retrieved via theFormConfig
attribute) into an HTML attribute for use by the front-end JS code. This provides a set-and-forget solution where I can happily nest as many prototyped forms as I need without concerning myself with the placeholders.Example implementation of a
CollectionType
extension that generates random placeholders: