Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Form] Add "is empty callback" to form config #32747

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 1 commit into from
Feb 5, 2020
Merged
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
8 changes: 8 additions & 0 deletions 8 UPGRADE-5.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ EventDispatcher

* Deprecated `LegacyEventDispatcherProxy`. Use the event dispatcher without the proxy.

Form
----

* Implementing the `FormConfigInterface` without implementing the `getIsEmptyCallback()` method
is deprecated. The method will be added to the interface in 6.0.
* Implementing the `FormConfigBuilderInterface` without implementing the `setIsEmptyCallback()` method
is deprecated. The method will be added to the interface in 6.0.

FrameworkBundle
---------------

Expand Down
6 changes: 6 additions & 0 deletions 6 UPGRADE-6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ EventDispatcher

* Removed `LegacyEventDispatcherProxy`. Use the event dispatcher without the proxy.

Form
----

* Added the `getIsEmptyCallback()` method to the `FormConfigInterface`.
* Added the `setIsEmptyCallback()` method to the `FormConfigBuilderInterface`.

FrameworkBundle
---------------

Expand Down
20 changes: 20 additions & 0 deletions 20 src/Symfony/Component/Form/ButtonBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,16 @@ public function getFormConfig()
return $config;
}

/**
* Unsupported method.
*
* @throws BadMethodCallException
*/
public function setIsEmptyCallback(?callable $isEmptyCallback)
{
throw new BadMethodCallException('Buttons do not support "is empty" callback.');
fancyweb marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Unsupported method.
*/
Expand Down Expand Up @@ -738,6 +748,16 @@ public function getOption(string $name, $default = null)
return \array_key_exists($name, $this->options) ? $this->options[$name] : $default;
}

/**
* Unsupported method.
*
* @throws BadMethodCallException
*/
public function getIsEmptyCallback(): ?callable
{
throw new BadMethodCallException('Buttons do not support "is empty" callback.');
fancyweb marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Unsupported method.
*
Expand Down
4 changes: 4 additions & 0 deletions 4 src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ CHANGELOG

* The `view_timezone` option defaults to the `model_timezone` if no `reference_date` is configured.
* Added default `inputmode` attribute to Search, Email and Tel form types.
* Implementing the `FormConfigInterface` without implementing the `getIsEmptyCallback()` method
is deprecated. The method will be added to the interface in 6.0.
* Implementing the `FormConfigBuilderInterface` without implementing the `setIsEmptyCallback()` method
is deprecated. The method will be added to the interface in 6.0.

5.0.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public function configureOptions(OptionsResolver $resolver)
'empty_data' => $emptyData,
'compound' => false,
'false_values' => [null],
'is_empty_callback' => static function ($modelData): bool {
return false === $modelData;
},
]);

$resolver->setAllowedTypes('false_values', 'array');
Expand Down
11 changes: 11 additions & 0 deletions 11 src/Symfony/Component/Form/Extension/Core/Type/FormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
use Symfony\Component\Form\Extension\Core\EventListener\TrimListener;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormConfigBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
Expand Down Expand Up @@ -58,6 +59,14 @@ public function buildForm(FormBuilderInterface $builder, array $options)
if ($options['trim']) {
$builder->addEventSubscriber(new TrimListener());
}

if (!method_exists($builder, 'setIsEmptyCallback')) {
@trigger_error(sprintf('Not implementing the "%s::setIsEmptyCallback()" method in "%s" is deprecated since Symfony 5.1.', FormConfigBuilderInterface::class, \get_class($builder)), E_USER_DEPRECATED);

return;
}

$builder->setIsEmptyCallback($options['is_empty_callback']);
fancyweb marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -190,13 +199,15 @@ public function configureOptions(OptionsResolver $resolver)
'help_attr' => [],
'help_html' => false,
'help_translation_parameters' => [],
'is_empty_callback' => null,
]);

$resolver->setAllowedTypes('label_attr', 'array');
$resolver->setAllowedTypes('upload_max_size_message', ['callable']);
$resolver->setAllowedTypes('help', ['string', 'null']);
$resolver->setAllowedTypes('help_attr', 'array');
$resolver->setAllowedTypes('help_html', 'bool');
$resolver->setAllowedTypes('is_empty_callback', ['null', 'callable']);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions 12 src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,18 @@ public function isEmpty()
}
}

if (!method_exists($this->config, 'getIsEmptyCallback')) {
@trigger_error(sprintf('Not implementing the "%s::getIsEmptyCallback()" method in "%s" is deprecated since Symfony 5.1.', FormConfigInterface::class, \get_class($this->config)), E_USER_DEPRECATED);

$isEmptyCallback = null;
} else {
$isEmptyCallback = $this->config->getIsEmptyCallback();
}

if (null !== $isEmptyCallback) {
return $isEmptyCallback($this->modelData);
}

return FormUtil::isEmpty($this->modelData) ||
// arrays, countables
((\is_array($this->modelData) || $this->modelData instanceof \Countable) && 0 === \count($this->modelData)) ||
Expand Down
19 changes: 19 additions & 0 deletions 19 src/Symfony/Component/Form/FormConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface

private $autoInitialize = false;
private $options;
private $isEmptyCallback;

/**
* Creates an empty form configuration.
Expand Down Expand Up @@ -461,6 +462,14 @@ public function getOption(string $name, $default = null)
return \array_key_exists($name, $this->options) ? $this->options[$name] : $default;
}

/**
* {@inheritdoc}
*/
public function getIsEmptyCallback(): ?callable
fancyweb marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->isEmptyCallback;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -761,6 +770,16 @@ public function getFormConfig()
return $config;
}

/**
* {@inheritdoc}
*/
public function setIsEmptyCallback(?callable $isEmptyCallback)
nicolas-grekas marked this conversation as resolved.
Show resolved Hide resolved
{
$this->isEmptyCallback = $isEmptyCallback;

return $this;
}

/**
* Validates whether the given variable is a valid form name.
*
Expand Down
2 changes: 2 additions & 0 deletions 2 src/Symfony/Component/Form/FormConfigBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @method $this setIsEmptyCallback(callable|null $isEmptyCallback) Sets the callback that will be called to determine if the model data of the form is empty or not - not implementing it is deprecated since Symfony 5.1
*/
interface FormConfigBuilderInterface extends FormConfigInterface
{
Expand Down
2 changes: 2 additions & 0 deletions 2 src/Symfony/Component/Form/FormConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* The configuration of a {@link Form} object.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @method callable|null getIsEmptyCallback() Returns a callable that takes the model data as argument and that returns if it is empty or not - not implementing it is deprecated since Symfony 5.1
*/
interface FormConfigInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2049,4 +2049,45 @@ public function provideTrimCases()
'Multiple expanded' => [true, true],
];
}

/**
* @dataProvider expandedIsEmptyWhenNoRealChoiceIsSelectedProvider
*/
public function testExpandedIsEmptyWhenNoRealChoiceIsSelected(bool $expected, $submittedData, bool $multiple, bool $required, $placeholder)
{
$options = [
'expanded' => true,
'choices' => [
'foo' => 'bar',
],
'multiple' => $multiple,
'required' => $required,
];

if (!$multiple) {
$options['placeholder'] = $placeholder;
}

$form = $this->factory->create(static::TESTED_TYPE, null, $options);

$form->submit($submittedData);

$this->assertSame($expected, $form->isEmpty());
}

public function expandedIsEmptyWhenNoRealChoiceIsSelectedProvider()
{
// Some invalid cases are voluntarily not tested:
// - multiple with placeholder
// - required with placeholder
return [
'Nothing submitted / single / not required / without a placeholder -> should be empty' => [true, null, false, false, null],
xabbuh marked this conversation as resolved.
Show resolved Hide resolved
'Nothing submitted / single / not required / with a placeholder -> should not be empty' => [false, null, false, false, 'ccc'], // It falls back on the placeholder
'Nothing submitted / single / required / without a placeholder -> should be empty' => [true, null, false, true, null],
'Nothing submitted / single / required / with a placeholder -> should be empty' => [true, null, false, true, 'ccc'],
'Nothing submitted / multiple / not required / without a placeholder -> should be empty' => [true, null, true, false, null],
'Nothing submitted / multiple / required / without a placeholder -> should be empty' => [true, null, true, true, null],
'Placeholder submitted / single / not required / with a placeholder -> should not be empty' => [false, '', false, false, 'ccc'], // The placeholder is a selected value
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"help_html",
"help_translation_parameters",
"inherit_data",
"is_empty_callback",
"label",
"label_attr",
"label_format",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Symfony\Component\Form\Extension\Core\Type\ChoiceType (Block prefix: "choice")
help_html
help_translation_parameters
inherit_data
is_empty_callback
label
label_attr
label_format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"help_html",
"help_translation_parameters",
"inherit_data",
"is_empty_callback",
"label",
"label_attr",
"label_format",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Symfony\Component\Form\Extension\Core\Type\FormType (Block prefix: "form")
help_html
help_translation_parameters
inherit_data
is_empty_callback
label
label_attr
label_format
Expand Down
15 changes: 15 additions & 0 deletions 15 src/Symfony/Component/Form/Tests/SimpleFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,21 @@ public function testCannotCallGetViewDataInPreSetDataListener()
$form->setData('foo');
}

public function testIsEmptyCallback()
{
$config = new FormConfigBuilder('foo', null, $this->dispatcher);

$config->setIsEmptyCallback(function ($modelData): bool { return 'ccc' === $modelData; });
$form = new Form($config);
$form->setData('ccc');
$this->assertTrue($form->isEmpty());

$config->setIsEmptyCallback(function (): bool { return false; });
$form = new Form($config);
$form->setData(null);
$this->assertFalse($form->isEmpty());
}

protected function createForm(): FormInterface
{
return $this->getBuilder()->getForm();
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.