8000 [Form] Renamed the option "empty_value" to "placeholder" by norberttech · Pull Request #6475 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Renamed the option "empty_value" to "placeholder" #6475

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@

{% block widget_attributes %}
{% spaceless %}
id="{{ id }}" name="{{ full_name }}"{% if read_only %} readonly="readonly"{% endif %}{% if disabled %} disabled="disabled"{% endif %}{% if required %} required="required"{% endif %}{% if max_length %} maxlength="{{ max_length }}"{% endif %}{% if pattern %} pattern="{{ pattern }}"{% endif %}
{% for attrname, attrvalue in attr %}{% if attrname in ['placeholder', 'title'] %}{{ attrname }}="{{ attrvalue|trans({}, translation_domain) }}" {% else %}{{ attrname }}="{{ attrvalue }}" {% endif %}{% endfor %}
id="{{ id }}" name="{{ full_name }}"{% if read_only %} readonly="readonly"{% endif %}{% if disabled %} disabled="disabled"{% endif %}{% if required %} required="required"{% endif %}{% if max_length %} maxlength="{{ max_length }}"{% endif %}{% if pattern %} pattern="{{ pattern }}"{% endif %}{% if placeholder is defined %} placeholder="{{ placeholder|trans({}, translation_domain) }}"{% endif %}
{% for attrname, attrvalue in attr %}{% if attrname in ['title'] %}{{ attrname }}="{{ attrvalue|trans({}, translation_domain) }}" {% else %}{{ attrname }}="{{ attrvalue }}" {% endif %}{% endfor %}
Copy link
Contributor

Choose a reason for hiding this comment

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

if 'title' == attrname

{% endspaceless %}
{% endblock widget_attributes %}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<?php if ($required): ?>required="required" <?php endif ?>
<?php if ($max_length): ?>maxlength="<?php echo $view->escape($max_length) ?>" <?php endif ?>
<?php if ($pattern): ?>pattern="<?php echo $view->escape($pattern) ?>" <?php endif ?>
<?php if ($placeholder): ?>placeholder="<?php echo $view->escape($view['translator']->trans($placeholder, array(), $translation_domain)) ?>" <?php endif ?>
<?php foreach ($attr as $k => $v): ?>
<?php printf('%s="%s" ', $view->escape($k), $view->escape(in_array($v, array('placeholder', 'title')) ? $view['translator']->trans($v, array(), $translation_domain) : $v)) ?>
<?php printf('%s="%s" ', $view->escape($k), $view->escape(in_array($v, array('title')) ? $view['translator']->trans($v, array(), $translation_domain) : $v)) ?>
Copy link
Contributor

Choose a reason for hiding this comment

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

$view->escape('title' === $v ? $view['translator]'...

<?php endforeach; ?>
21 changes: 13 additions & 8 deletions src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,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('')))) {
$view->vars['empty_value'] = $options['empty_value'];
$view->vars['empty_value'] = $options['placeholder'];
Copy link
Contributor

Choose a reason for hiding this comment

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

the variable empty_value should be kept for BC

}

if ($options['multiple'] && !$options['expanded']) {
Expand Down Expand Up @@ -165,21 +165,24 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
return '';
};

$emptyValue = function (Options $options) {
return $options['required'] ? null : '';
$placeholderValue = function (Options $options) {
// BC until Symfony 2.3
return null !== $options['empty_value']
? $options['empty_value']
: ($options['required'] ? null : '');
};

$emptyValueNormalizer = function (Options $options, $emptyValue) {
$placeholderValueNormalizer = function (Options $options, $placeholderValue) {
if ($options['multiple'] || $options['expanded']) {
// never use an empty value for these cases
return null;
} elseif (false === $emptyValue) {
} elseif (false === $placeholderValue) {
// an empty value should be added but the user decided otherwise
return null;
}

// empty value has been set explicitly
return $emptyValue;
return $placeholderValue;
};

$compound = function (Options $options) {
Expand All @@ -193,7 +196,9 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'choices' => array(),
'preferred_choices' => array(),
'empty_data' => $emptyData,
'empty_value' => $emptyValue,
'placeholder' => $placeholderValue,
Copy link
Contributor

Choose a reason for hiding this comment

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

The "placeholder" option should be removed here and added to FormType instead.

// Deprecated empty_value option
'empty_value' => null,
'error_bubbling' => false,
'compound' => $compound,
// The view data is always a string, even if the "data" option
Expand All @@ -203,7 +208,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
));

$resolver->setNormalizers(array(
'empty_value' => $emptyValueNormalizer,
'placeholder' => $placeholderValueNormalizer,
));

$resolver->setAllowedTypes(array(
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'years',
'months',
'days',
'placeholder',
'empty_value',
'required',
'translation_domain',
Expand All @@ -125,6 +126,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'seconds',
'with_minutes',
'with_seconds',
'placeholder',
'empty_value',
'required',
'translation_domain',
Expand Down Expand Up @@ -217,7 +219,15 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
return $options['user_timezone'];
};

$placeholderValue = function (Options $options) {
// BC until Symfony 2.3
return isset($options['empty_value'])
? $options['empty_value']
: 23D3 ($options['required'] ? null : '');
};

$resolver->setDefaults(array(
'placeholder' => $placeholderValue,
'input' => 'datetime',
'model_timezone' => $modelTimezone,
'view_timezone' => $viewTimezone,
Expand Down Expand Up @@ -246,6 +256,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
// Don't add some defaults in order to preserve the defaults
// set in DateType and TimeType
$resolver->setOptional(array(
// Deprecated empty_value option
'empty_value',
'years',
'months',
Expand Down
35 changes: 22 additions & 13 deletions src/Symfony/Component/Form/Extension/Core/Type/DateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ public function buildForm(FormBuilderInterface $builder, array $options)
if ('choice' === $options['widget']) {
// Only pass a subset of the options to children
$yearOptions['choices'] = $this->formatTimestamps($formatter, '/y+/', $this->listYears($options['years']));
$yearOptions['empty_value'] = $options['empty_value']['year'];
$yearOptions['placeholder'] = $options['placeholder']['year'];
$monthOptions['choices'] = $this->formatTimestamps($formatter, '/[M|L]+/', $this->listMonths($options['months']));
$monthOptions['empty_value'] = $options['empty_value']['month'];
$monthOptions['placeholder'] = $options['placeholder']['month'];
$dayOptions['choices'] = $this->formatTimestamps($formatter, '/d+/', $this->listDays($options['days']));
$dayOptions['empty_value'] = $options['empty_value']['day'];
$dayOptions['placeholder'] = $options['placeholder']['day'];
}

// Append generic carry-along options
Expand Down Expand Up @@ -164,24 +164,31 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
return $options['widget'] !== 'single_text';
};

$emptyValue = $emptyValueDefault = function (Options $options) {
$placeholderValue = function (Options $options) {
// BC until Symfony 2.3
return $options['empty_value'] !== null
? $options['empty_value']
: ($options['required'] ? null : '');
};

$placeholderValueDefault = function (Options $options) {
return $options['required'] ? null : '';
};

$emptyValueNormalizer = function (Options $options, $emptyValue) use ($emptyValueDefault) {
if (is_array($emptyValue)) {
$default = $emptyValueDefault($options);
$placeholderValueNormalizer = function (Options $options, $placeholderValue) use ($placeholderValueDefault) {
if (is_array($placeholderValue)) {
$default = $placeholderValueDefault($options);

return array_merge(
array('year' => $default, 'month' => $default, 'day' => $default),
$emptyValue
$placeholderValue
);
}

return array(
'year' => $emptyValue,
'month' => $emptyValue,
'day' => $emptyValue
'year' => $placeholderValue,
'month' => $placeholderValue,
'day' => $placeholderValue
);
};

Expand Down Expand Up @@ -211,7 +218,9 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
// Deprecated timezone options
'data_timezone' => null,
'user_timezone' => null,
'empty_value' => $emptyValue,
'placeholder' => $placeholderValue,
// Deprecated empty_value option
'empty_value' => null,
// Don't modify \DateTime classes by reference, we treat
// them like immutable value objects
'by_reference' => false,
Expand All @@ -225,7 +234,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
));

$resolver->setNormalizers(array(
'empty_value' => $emptyValueNormalizer,
'placeholder' => $placeholderValueNormalizer,
));

$resolver->setAllowedValues(array(
Expand Down
22 changes: 22 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/TextType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,39 @@
namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class TextType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars = array_replace($view->vars, array(
'placeholder' => $options['placeholder']
));
}

/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$placeholder = function (Options $options) {
if (isset($options['attr']['placeholder'])) {
return $options['attr']['placeholder'];
}

return null;
};

$resolver->setDefaults(array(
'compound' => false,
'placeholder' => $placeholder
));
}

Expand Down
35 changes: 22 additions & 13 deletions src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ public function buildForm(FormBuilderInterface $builder, array $options)

// Only pass a subset of the options to children
$hourOptions['choices'] = $hours;
$hourOptions['empty_value'] = $options['empty_value']['hour'];
$hourOptions['placeholder'] = $options['placeholder']['hour'];

if ($options['with_minutes']) {
foreach ($options['minutes'] as $minute) {
$minutes[$minute] = str_pad($minute, 2, '0', STR_PAD_LEFT);
}

$minuteOptions['choices'] = $minutes;
$minuteOptions['empty_value'] = $options['empty_value']['minute'];
$minuteOptions['placeholder'] = $options['placeholder']['minute'];
}

if ($options['with_seconds']) {
Expand All @@ -82,7 +82,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
}

$secondOptions['choices'] = $seconds;
$secondOptions['empty_value'] = $options['empty_value']['second'];
$secondOptions['placeholder'] = $options['placeholder']['second'];
}

// Append generic carry-along options
Expand Down Expand Up @@ -152,24 +152,31 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
return $options['widget'] !== 'single_text';
};

$emptyValue = $emptyValueDefault = function (Options $options) {
$placeholderValue = function (Options $options) {
// BC until Symfony 2.3
return $options['empty_value'] !== null
? $options['empty_value']
: ($options['required'] ? null : '');
};

$placeholderValueDefault = function (Options $options) {
return $options['required'] ? null : '';
};

$emptyValueNormalizer = function (Options $options, $emptyValue) use ($emptyValueDefault) {
if (is_array($emptyValue)) {
$default = $emptyValueDefault($options);
$placeholderValueNormalizer = function (Options $options, $placeholderValue) use ($placeholderValueDefault) {
if (is_array($placeholderValue)) {
$default = $placeholderValueDefault($options);

return array_merge(
array('hour' => $default, 'minute' => $default, 'second' => $default),
$emptyValue
$placeholderValue
);
}

return array(
'hour' => $emptyValue,
'minute' => $emptyValue,
'second' => $emptyValue
'hour' => $placeholderValue,
'minute' => $placeholderValue,
'second' => $placeholderValue
);
};

Expand All @@ -196,7 +203,9 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
// Deprecated timezone options
'data_timezone' => null,
'user_timezone' => null,
'empty_value' => $emptyValue,
'placeholder' => $placeholderValue,
// Deprecated empty_value option
'empty_value' => null,
// Don't modify \DateTime classes by reference, we treat
// them like immutable value objects
'by_reference' => false,
Expand All @@ -210,7 +219,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)< 7802 /td>
));

$resolver->setNormalizers(array(
'empty_value' => $emptyValueNormalizer,
'placeholder' => $placeholderValueNormalizer,
));

$resolver->setAllowedValues(array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,23 @@ public function testPassEmptyValueToView($multiple, $expanded, $required, $empty
$this->assertEquals($viewValue, $view->vars['empty_value']);
}

/**
* @dataProvider getOptionsWithEmptyValue
*/
public function testPassPlaceholderToView($multiple, $expanded, $required, $placeholderValue, $viewValue)
{
$form = $this->factory->create('choice', null, array(
'multiple' => $multiple,
'expanded' => $expanded,
'required' => $required,
'placeholder' => $placeholderValue,
'choices' => $this->choices,
));
$view = $form->createView();

$this->assertEquals($viewValue, $view->vars['empty_value']);
}

/**
* @dataProvider getOptionsWithEmptyValue
*/
Expand All @@ -630,6 +647,23 @@ public function testDontPassEmptyValueIfContainedInChoices($multiple, $expanded,
$this->assertNull($view->vars['empty_value']);
}

/**
* @dataProvider getOptionsWithEmptyValue
*/
public function testDontPassPlaceholderIfContainedInChoices($multiple, $expanded, $required, $placeholderValue, $viewValue)
{
$form = $this->factory->create('choice', null, array(
'multiple' => $multiple,
'expanded' => $expanded,
'required' => $required,
'placeholder' => $placeholderValue,
'choices' => array('a' => 'A', '' => 'Empty'),
));
$view = $form->createView();

$this->assertNull($view->vars['empty_value']);
}

public function getOptionsWithEmptyValue()
{
return array(
Expand Down
Loading
0