-
-
Notifications
You must be signed in to change notification settings - Fork 399
UX FormCollection #90
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
Changes from 1 commit
ae5d325
c305f0c
5e6dd84
eea2313
f651d90
cc02076
dceb2cf
dfb54f8
9300cda
ec4342a
b4f40cd
0584757
abd2b8f
3996f3b
aaa6811
4791ff9
8539ff9
2d1ed04
1e3105d
0a9cc54
94be94c
c805c0e
e56d04b
7467cee
a8c730c
ecd774a
25d8306
8e5cd8a
3b110f9
c36157c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -20,6 +20,8 @@ yarn encore dev | |||||
| Also make sure you have at least version 2.0 of [@symfony/stimulus-bridge](https://github.com/symfony/stimulus-bridge) | ||||||
| in your `package.json` file. | ||||||
|
|
||||||
| ## Use predefined theme | ||||||
|
|
||||||
| You need to select the right theme from the one you are using : | ||||||
| ```yaml | ||||||
| # config/packages/twig.yaml | ||||||
|
|
@@ -33,6 +35,42 @@ You have 2 different themes : | |||||
|
|
||||||
| [Check the Symfony doc](https://symfony.com/doc/4.4/form/form_themes.html) for the different ways to set themes in Symfony. | ||||||
|
|
||||||
| ## Use manual theming | ||||||
|
|
||||||
| > Consider your `BlogFormType` form set up and with a comments field that is a `CollectionType`, you can | ||||||
stakovicz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| render it in your template: | ||||||
|
|
||||||
| ```twig | ||||||
| {% macro commentFormRow(commentForm) %} | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why a macro instead of a "real" form theme?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's @weaverryan's idea. It is easier and more flexible to implement. |
||||||
| <div | ||||||
| class="col-4" | ||||||
| data-symfony--ux-form-collection--collection-target="entry" | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should show |
||||||
| > | ||||||
| {{ form_errors(commentForm) }} | ||||||
| {{ form_row(commentForm.content) }} | ||||||
| {{ form_row(commentForm.otherField) }} | ||||||
|
|
||||||
| <button type="button" data-action="symfony--ux-form-collection--collection#delete"> | ||||||
| Remove | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use option text from the FormType?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| </button> | ||||||
| </div> | ||||||
| {% endmacro %} | ||||||
|
|
||||||
| <div | ||||||
| class="row" | ||||||
| data-controller="symfony--ux-form-collection--collection" | ||||||
| data-prototype="{{ _self.commentFormRow(form.comments.vars.prototype)|e }}" | ||||||
| > | ||||||
| {% for commentForm in form.comments %} | ||||||
| {{ _self.commentFormRow(commentForm) }} | ||||||
| {% endfor %} | ||||||
|
|
||||||
| <button type="button" data-action="symfony--ux-form-collection--collection#add"> | ||||||
| Add Another | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use option text from the FormType?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| </button> | ||||||
| </div> | ||||||
| ``` | ||||||
|
|
||||||
| ## Usage | ||||||
|
|
||||||
| The most common usage of Form Collection is to use it as a replacement of | ||||||
|
|
@@ -51,15 +89,15 @@ class BlogFormType extends AbstractType | |||||
| ->add('comments', UXCollectionType::class, [ | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be as helpful as possible, I think we need to show the user a simple Overall, this is how my example looks locally - I think these options are helpful to all show! 'allow_add' => true,
'allow_delete' => true,
'entry_type' => FileType::class,
// ideally UXCollectionType can set this for us
'by_reference' => false,
'entry_options' => [
// hides the "0", "1", "2" labels on the embedded forms
'label' => false,
],
'button_add_options' => [
'label' => '<span class="fa fa-plus"></span>',
'label_html' => true,
'class' => 'btn btn-secondary btn-sm'
],
'button_delete_options' => [
'label' => '<span class="fa fa-times"></span>',
'label_html' => true,
'class' => 'btn btn-danger btn-sm'
], |
||||||
| // ... | ||||||
| 'button_add' => [ | ||||||
| // Default text for the add button | ||||||
| // Default text for the add button (used by predefined theme) | ||||||
| 'text' => 'Add', | ||||||
| // Add HTML classes to the add button | ||||||
| // Add HTML classes to the add button (used by predefined theme) | ||||||
| 'class' => 'btn btn-outline-primary' | ||||||
| ], | ||||||
| 'button_delete' => [ | ||||||
| // Default text for the delete button | ||||||
| // Default text for the delete button (used by predefined theme) | ||||||
| 'text' => 'Remove', | ||||||
| // Add HTML classes to the add button | ||||||
| // Add HTML classes to the add button (used by predefined theme) | ||||||
| 'class' => 'btn btn-outline-secondary' | ||||||
| ], | ||||||
| ]) | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,31 +18,31 @@ | |
| {# attr for the data target on the entry of the collection #} | ||
| {%- set attrDataTarget = {('data-' ~ controllerName ~ '-target'): 'entry' } -%} | ||
|
|
||
| {% if prototype is defined and not prototype.rendered %} | ||
| {%- set prototype_attr = prototype.vars.attr|merge(attrDataTarget) -%} | ||
stakovicz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| {%- set attr = attr|merge({'data-prototype': form_row(prototype, {'row_attr': prototype_attr}) }) -%} | ||
| {% endif %} | ||
| {%- set attr = attr|merge({('data-' ~ controllerName ~ '-target'): 'container' }) -%} | ||
|
|
||
| <div data-controller="{{ dataController }}" | ||
| data-{{ controllerName }}-allow-add-value="{{ allow_add|json_encode }}" | ||
| data-{{ controllerName }}-allow-delete-value="{{ allow_delete|json_encode }}" | ||
| data-{{ controllerName }}-button-add-value="{{ block('button_add')|e }}" | ||
| data-{{ controllerName }}-button-delete-value="{{ block('button_delete')|e }}" | ||
| data-{{ controllerName }}-prototype-name-value="{{ prototype_name }}" | ||
| {{ block('widget_container_attributes') }} | ||
| > | ||
| {% if prototype is defined and not prototype.rendered %} | ||
| {%- set prototype_attr = prototype.vars.attr|merge(attrDataTarget) -%} | ||
| {%- set attr = attr|merge({'data-prototype': form_row(prototype, {'row_attr': prototype_attr}) }) -%} | ||
| {% endif %} | ||
| {%- set attr = attr|merge({('data-' ~ controllerName ~ '-target'): 'container' }) -%} | ||
| {%- if form is rootform -%} | ||
| {{ form_errors(form) }} | ||
| {%- endif -%} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's this for? |
||
|
|
||
| <div {{ block('widget_container_attributes') }}> | ||
| {%- if form is rootform -%} | ||
| {{ form_errors(form) }} | ||
| {%- endif -%} | ||
| {% for child in form|filter(child => not child.rendered) %} | ||
|
|
||
| {% for child in form|filter(child => not child.rendered) %} | ||
| {%- set child_attr = child.vars.attr|merge(attrDataTarget) -%} | ||
stakovicz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| {{- form_row(child, {'row_attr': child_attr}) -}} | ||
|
|
||
| {%- set child_attr = child.vars.attr|merge(attrDataTarget) -%} | ||
| {{- form_row(child, {'row_attr': child_attr}) -}} | ||
| {% endfor %} | ||
|
|
||
| {% endfor %} | ||
| </div> | ||
| {{- form_rest(form) -}} | ||
| </div> | ||
| {%- endblock %} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,20 +22,20 @@ | |
| {# attr for the data target on the entry of the collection #} | ||
| {%- set attrDataTarget = {('data-' ~ controllerName ~ '-target'): 'entry' } -%} | ||
|
|
||
| {% if prototype is defined and not prototype.rendered %} | ||
| {%- set prototype_attr = prototype.vars.attr|merge(attrDataTarget) -%} | ||
stakovicz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| {%- set attr = attr|merge({'data-prototype': form_row(prototype, {'row_attr': prototype_attr}) }) -%} | ||
| {% endif %} | ||
|
|
||
| <div data-controller="{{ dataController }}" | ||
| data-{{ controllerName }}-allow-add-value="{{ allow_add|json_encode }}" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I'm personally missing is a |
||
| data-{{ controllerName }}-allow-delete-value="{{ allow_delete|json_encode }}" | ||
| data-{{ controllerName }}-button-add-value="{{ block('button_add')|e }}" | ||
| data-{{ controllerName }}-button-delete-value="{{ block('button_delete')|e }}" | ||
| data-{{ controllerName }}-prototype-name-value="{{ prototype_name }}" | ||
| {{ block('widget_container_attributes') }} | ||
| > | ||
| {% if prototype is defined and not prototype.rendered %} | ||
| {%- set prototype_attr = prototype.vars.attr|merge(attrDataTarget) -%} | ||
| {%- set attr = attr|merge({'data-prototype': form_row(prototype, {'row_attr': prototype_attr}) }) -%} | ||
| {% endif %} | ||
| {%- set attr = attr|merge({('data-' ~ controllerName ~ '-target'): 'container' }) -%} | ||
|
|
||
| <table {{ block('widget_container_attributes') }}> | ||
| <table> | ||
| {%- if form is rootform -%} | ||
| {{ form_errors(form) }} | ||
| {%- endif -%} | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.