-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[WIP] [3.2] [Form] add 'empty_view' option in FormType #17609
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 all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,3 +42,14 @@ | |
{{- form_rest(form) -}} | ||
</table> | ||
{%- endblock form_widget_compound -%} | ||
|
||
{%- block empty_row -%} | ||
{%- set translation_domain = translation_domain ?: 'sf_form' -%} | ||
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 do you set this translation domain? 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 see now. Shouldn't we rather call the catalog 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 was a suggestion that we might provide default translations for "No choices..", "No Fields" and even the actual "None" as default placeholder like it's done for validation message, there is already the dependency. I can remove it of courses :) 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. Yes I was just not sure it will not conflict with other catalogs with the same name in users code. |
||
<table {{ block('widget_container_attributes') }}> | ||
<tr> | ||
<td colspan="2"> | ||
<em>{{ translation_domain is same as(false) ? empty_view : empty_view|trans({}, translation_domain) }}</em> | ||
</td> | ||
</tr> | ||
</table> | ||
{%- endblock empty_row -%} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
<?php if ($expanded): ?> | ||
<?php echo $view['form']->block($form, 'choice_widget_expanded') ?> | ||
<?php if (isset($empty_view) && $empty_view): ?> | ||
<?php echo $view['form']->block($form, 'empty_row')?> | ||
<?php else: ?> | ||
<?php echo $view['form']->block($form, 'choice_widget_collapsed') ?> | ||
<?php if ($expanded): ?> | ||
<?php echo $view['form']->block($form, 'choice_widget_expanded') ?> | ||
<?php else: ?> | ||
<?php echo $view['form']->block($form, 'choice_widget_collapsed') ?> | ||
<?php endif ?> | ||
<?php endif ?> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php $translation_domain = $translation_domain ?: 'sf_form'; ?> | ||
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. same here |
||
<div <?php echo $view['form']->block($form, 'widget_container_attributes') ?>> | ||
<em> | ||
<?php echo $view->escape(false !== $translation_domain | ||
? $view['translator']->trans($empty_view, array(), $translation_domain) | ||
: $empty_view | ||
); ?> | ||
</em> | ||
</div> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
<?php if ($compound): ?> | ||
<?php echo $view['form']->block($form, 'form_widget_compound')?> | ||
<?php if (isset($empty_view) && $empty_view): ?> | ||
<?php echo $view['form']->block($form, 'empty_row')?> | ||
<?php echo $view['form']->rest($form) ?> | ||
<?php else: ?> | ||
<?php echo $view['form']->block($form, 'form_widget_compound')?> | ||
<?php endif ?> | ||
<?php else: ?> | ||
<?php echo $view['form']->block($form, 'form_widget_simple')?> | ||
<?php endif ?> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php $translation_domain = $translation_domain ?: 'sf_form'; ?> | ||
<table <?php echo $view['form']->block($form, 'widget_container_attributes') ?>> | ||
<tr> | ||
<td colspan="2"> | ||
<em> | ||
<?php echo $view->escape(false !== $translation_domain | ||
? $view['translator']->trans($empty_view, array(), $translation_domain) | ||
: $empty_view | ||
); ?> | ||
</em> | ||
</td> | ||
</tr> | ||
</table> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -244,6 +244,18 @@ public function finishView(FormView $view, FormInterface $form, array $options) | |
$childView->vars['full_name'] = $childName; | ||
} | ||
} | ||
|
||
// We have to override parent {@link FormType} condition of the empty_view option here, | ||
// even if there is no choices the field view may not be empty because: | ||
// | ||
// - a select input is not "compound" but should be considered as any empty HTML tag, | ||
// | ||
// - when expanded it could hold a placeholder which has been counted as a children, | ||
// we should ignore it in any case since its role is to be a "null" alternative to | ||
// choices, not a choice itself. Think of "Choose something" with no choices. | ||
if (array() === $view->vars['choices'] && array() === $view->vars['preferred_choices']) { | ||
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 is this check needed? Can't we decide in the view whether to output the text? 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. @webmozart Because the view var I don't know how BC could be broken with such changes in form themes. the fact that I modify several test with the current implementation is not really reassuring. What do you think ? |
||
$view->vars['empty_view'] = $options['empty_view']; | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -323,6 +335,7 @@ public function configureOptions(OptionsResolver $resolver) | |
'preferred_choices' => array(), | ||
'group_by' => null, | ||
'empty_data' => $emptyData, | ||
'empty_view' => 'No choice available', | ||
'placeholder' => $placeholderDefault, | ||
'error_bubbling' => false, | ||
'compound' => $compound, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,6 +113,10 @@ public function finishView(FormView $view, FormInterface $form, array $options) | |
} | ||
|
||
$view->vars['multipart'] = $multipart; | ||
|
||
if ($form->getConfig()->getCompound()) { | ||
$view->vars['empty_view'] = (bool) $view->count() ? false : $options['empty_view']; | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -142,6 +146,12 @@ public function configureOptions(OptionsResolver $resolver) | |
}; | ||
}; | ||
|
||
// For any compound form which has no children, show some text | ||
// instead of an empty HTML tag by default | ||
$emptyView = function (Options $options) { | ||
return $options['compound'] ? 'No fields' : false; | ||
}; | ||
|
||
// For any form that is not represented by a single HTML control, | ||
// errors should bubble up by default | ||
$errorBubbling = function (Options $options) { | ||
|
@@ -157,6 +167,7 @@ public function configureOptions(OptionsResolver $resolver) | |
$resolver->setDefaults(array( | ||
'data_class' => $dataClass, | ||
'empty_data' => $emptyData, | ||
'empty_view' => $emptyView, | ||
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 about naming this 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. Sounds good :) |
||
'trim' => true, | ||
'required' => true, | ||
'property_path' => null, | ||
|
@@ -175,6 +186,7 @@ public function configureOptions(OptionsResolver $resolver) | |
)); | ||
|
||
$resolver->setAllowedTypes('label_attr', 'array'); | ||
$resolver->setAllowedTypes('empty_view', array('null', 'string', 'bool')); | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0"?> | ||
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"> | ||
<file source-language="en" datatype="plaintext" original="file.ext"> | ||
<body> | ||
<trans-unit id="1"> | ||
<source>No choice available</source> | ||
<target>No choice available</target> | ||
</trans-unit> | ||
</body> | ||
</file> | ||
</xliff> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0"?> | ||
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"> | ||
<file source-language="en" datatype="plaintext" original="file.ext"> | ||
<body> | ||
<trans-unit id="1"> | ||
<source>No choice available</source> | ||
<target>Aucun choix disponible</target> | ||
</trans-unit> | ||
</body> | ||
</file> | ||
</xliff> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why
<em>
? I think we should refrain from adding styling as much as possible.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
em isnt about styling? its about emphasis. and because there is no form, that one might expect, you put the emphasis on the presented message, probably giving reasons as for why there are no children.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, I did not even think about it when I added it naturally.
Because, it may be unexpected to see that string standing for a missing expectation.
The
emphasis
clearly mark the informational sense of such string IMHO.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO we should leave that decision up to the developer. We never made such assumptions in any other part of form themes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough then :)