-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Form] Added radio button for empty value to expanded single-choice fields #7939
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Form] Added radio button for empty value to expanded single-choice f…
…ields
- Loading branch information
commit 793397198a3edb4b58a022e88b0710537dcec4c1
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,6 +95,8 @@ public function getRemainingViews(); | |
/** | ||
* Returns the choices corresponding to the given values. | ||
* | ||
* The choices can have any data type. | ||
* | ||
* @param array $values An array of choice values. Not existing values in | ||
* this array are ignored. | ||
* | ||
|
@@ -105,6 +107,8 @@ public function getChoicesForValues(array $values); | |
/** | ||
* Returns the values corresponding to the given choices. | ||
* | ||
* The values must be a strings. | ||
* | ||
* @param array $choices An array of choices. Not existing choices in this | ||
* array are ignored. | ||
* | ||
|
@@ -116,20 +120,30 @@ public function getValuesForChoices(array $choices); | |
/** | ||
* Returns the indices corresponding to the given choices. | ||
* | ||
* The indices must be positive integers or strings accepted by | ||
* {@link FormConfigBuilder::validateName()}. | ||
* | ||
* The index "placeholder" is internally reserved. | ||
* | ||
* @param array $choices An array of choices. Not existing choices in this | ||
* array are ignored. | ||
* | ||
* @return array An array of indices with ascending, 0-based numeric keys | ||
* @return array An array of indices with ascending, 0-based numeric keys. | ||
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 don't put dots at the end of the @... lines |
||
*/ | ||
public function getIndicesForChoices(array $choices); | ||
|
||
/** | ||
* Returns the indices corresponding to the given values. | ||
* | ||
* The indices must be positive integers or strings accepted by | ||
* {@link FormConfigBuilder::validateName()}. | ||
* | ||
* The index "placeholder" is internally reserved. | ||
* | ||
* @param array $values An array of choice values. Not existing values in | ||
* this array are ignored. | ||
* | ||
* @return array An array of indices with ascending, 0-based numeric keys | ||
* @return array An array of indices with ascending, 0-based numeric keys. | ||
*/ | ||
public function getIndicesForValues(array $values); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
8000
|
@@ -12,6 +12,7 @@ | |
namespace Symfony\Component\Form\Extension\Core\Type; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\View\ChoiceView; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\Form\FormView; | ||
|
@@ -45,19 +46,34 @@ public function buildForm(FormBuilderInterface $builder, array $options) | |
} | ||
|
||
if ($options['expanded']) { | ||
$this->addSubForms($builder, $options['choice_list']->getPreferredViews(), $options); | ||
$this->addSubForms($builder, $options['choice_list']->getRemainingViews(), $options); | ||
// Initialize all choices before doing the index check below. | ||
// This helps in cases where index checks are optimized for non | ||
// initialized choice lists. For example, when using an SQL driver, | ||
// the index check would read in one SQL query and the initialization | ||
// requires another SQL query. When the initialization is done first, | ||
// one SQL query is sufficient. | ||
$preferredViews = $options['choice_list']->getPreferredViews(); | ||
$remainingViews = $options['choice_list']->getRemainingViews(); | ||
|
||
// Check if the choices already contain the empty value | ||
// Only add the empty value option if this is not the case | ||
if (null !== $options['empty_value'] && 0 === count($options['choice_list']->getIndicesForValues(array('')))) { | ||
$placeholderView = new ChoiceView(null, '', $options['empty_value']); | ||
|
||
// "placeholder" is a reserved index | ||
// see also ChoiceListInterface::getIndicesForChoices() | ||
$this->addSubForms($builder, array('placeholder' => $placeholderView), $options); | ||
} | ||
|
||
$this->addSubForms($builder, $preferredViews, $options); | ||
$this->addSubForms($builder, $remainingViews, $options); | ||
|
||
if ($options['multiple']) { | ||
$builder | ||
->addViewTransformer(new ChoicesToBooleanArrayTransformer($options['choice_list'])) | ||
->addEventSubscriber(new FixCheckboxInputListener($options['choice_list']), 10) | ||
; | ||
$builder->addViewTransformer(new ChoicesToBooleanArrayTransformer($options['choice_list'])); | ||
$builder->addEventSubscriber(new FixCheckboxInputListener($options['choice_list']), 10); | ||
} else { | ||
$builder | ||
->addViewTransformer(new ChoiceToBooleanArrayTransformer($options['choice_list'])) | ||
->addEventSubscriber(new FixRadioInputListener($options['choice_list']), 10) | ||
; | ||
$builder->addViewTransformer(new ChoiceToBooleanArrayTransformer($options['choice_list'], $builder->has('placeholder'))); | ||
$builder->addEventSubscriber(new FixRadioInputListener($options['choice_list'], $builder->has('placeholder')), 10); | ||
} | ||
} else { | ||
if ($options['multiple']) { | ||
|
@@ -104,7 +120,7 @@ public function buildView(FormView $view, FormInterface $form, array $options) | |
|
||
// Check if the choices already contain the empty value | ||
// Only add the empty value option if this is not the case | ||
if (0 === count($options['choice_list']->getIndicesForValues(array('')))) { | ||
if (null !== $options['empty_value'] && 0 === count($options['choice_list']->getIndicesForValues(array('')))) { | ||
$view->vars['empty_value'] = $options['empty_value']; | ||
} | ||
|
||
|
@@ -170,12 +186,15 @@ public function setDefaultOptions(OptionsResolverInterface $resolver) | |
}; | ||
|
||
$emptyValueNormalizer = function (Options $options, $emptyValue) { | ||
if ($options['multiple'] || $options['expanded']) { | ||
// never use an empty value for these cases | ||
if ($options['multiple']) { | ||
// never use an empty value for this case | ||
return null; | ||
} elseif (false === $emptyValue) { | ||
// an empty value should be added but the user decided otherwise | ||
return null; | ||
} elseif ($options['expanded'] && '' === $emptyValue) { | ||
// never use an empty label for radio buttons | ||
return 'None'; | ||
} | ||
|
||
// empty value has been set explicitly | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
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.
Remove "a" here?