Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

[Form] Allow pass filter callback to delete_empty option.#20496

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:3.4fromKoc:form-is-empty-callback
Jul 26, 2017

Conversation

Koc
Copy link
Contributor

@KocKoc commentedNov 11, 2016
edited
Loading

QA
Branch?master
Bug fix?yes
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets#13601,#13940,#22008,#22014
LicenseMIT
Doc PRcoming soon

yceruto reacted with thumbs up emoji
Copy link
Contributor

@HeahDudeHeahDude left a comment

Choose a reason for hiding this comment

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

I like this, thanks for your proposal!

$isNew = !isset($previousData[$name]);

// $isNew can only be true if allowAdd is true, so we don't
// need to check allowAdd again
if ($child->isEmpty() && ($isNew || $this->allowDelete)) {
if (($child->isEmpty() || (null !== $isEmptyCallback && $isEmptyCallback($child->getData()))) && ($isNew || $this->allowDelete)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would add a variable above to ease readability:

/* @var $child FormInterface */$isNew = !isset($previousData[$name]);$isEmpty =$child->isEmpty() || (null !==$isEmptyCallback &&$isEmptyCallback($child->getData());// $isNew can only be true if allowAdd is true, so we don't// need to check allowAdd againif ($isEmpty && ($isNew ||$this->allowDelete)) {

Koc reacted with thumbs up emoji
@@ -97,6 +98,7 @@ public function configureOptions(OptionsResolver $resolver)
'entry_type' => __NAMESPACE__.'\TextType',
'entry_options' => array(),
'delete_empty' => false,
'is_empty_callback' => null,
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add:

$resolver->setAllowedTypes('is_empty_callback',array('null','callable'));

Koc reacted with thumbs up emoji
@KocKocforce-pushed theform-is-empty-callback branch 3 times, most recently fromd542c63 toc3e6ff5CompareNovember 12, 2016 10:50
@Koc
Copy link
ContributorAuthor

Koc commentedNov 12, 2016
edited
Loading

@HeahDude done. Also I've changed the test for better description of this new feature.

@nicolas-grekasnicolas-grekas added this to the3.x milestoneDec 6, 2016
@Koc
Copy link
ContributorAuthor

Koc commentedDec 26, 2016

can anybody review and merge?

@HeahDude
Copy link
Contributor

Some may have some concern about the naming of this option, for me it's a 👍

Status: Reviewed

@xabbuh
Copy link
Member

I agree with the implementation, but the option name doesn't sound good. For example, we don't check if the whole collection is empty, but only check for each submitted entry if it should be ignored. Can we think of a better name?

@Koc
Copy link
ContributorAuthor

Koc commentedFeb 8, 2017

give some examples of a better name, I'll rename it

@xabbuh
Copy link
Member

What about something likefilter_entry as we are basically filter our collection entries inside the callback?

HeahDude reacted with thumbs up emoji

@murilolobato
Copy link

Ifis_empty_callback is a option, I would rename it to justis_empty.

sirgallifrey reacted with thumbs up emoji

@fabpot
Copy link
Member

@Koc What about@xabbuh suggestion for the name?

@KocKocforce-pushed theform-is-empty-callback branch 4 times, most recently fromd9703d6 tocbd5707CompareMarch 23, 2017 11:14
@Koc
Copy link
ContributorAuthor

Koc commentedMar 23, 2017

@fabpot done, except I preferentry_filter to be consistent with other options likeentry_type/entry_options. Callable should return true for leave entry in collection.

Test failure is unrelated.

@KocKocforce-pushed theform-is-empty-callback branch fromcbd5707 to57a2aa5CompareMarch 23, 2017 12:54
@KocKoc changed the title[Form] Added is_empty_callback option to collection type.[Form] Added entry_filter option to collection type.Mar 23, 2017
@yceruto
Copy link
Member

yceruto commentedMar 23, 2017
edited
Loading

IMHO, the option should be the samedelete_empty, accepting bool or callable. Usetrue to delete empty (non-compound) entries, and use a callable function to delete empty (compound with or withoutdata_class) entries likeAuthor(null) orarray('firstName' => '').

In case of compound entries withdata_class:

'delete_empty' =>function (Author$author =null) {returnnull ===$author ||empty($author->getFirstName());},

In case of compound entries withoutdata_class:

'delete_empty' =>function ($author) {returnempty($author['firstName']);},

Thus, we avoid problems to use a new optionentry_filter (learning curve!!) which will depend ofdelete_empty = true also.

Koc, apfelbox, and ogizanagi reacted with thumbs up emojimurilolobato reacted with hooray emoji

@murilolobato
Copy link

When the property gets renamed, I can add the test I've made in#22014

$isNew = !isset($previousData[$name]);
$needRemove = $child->isEmpty() || (null !== $entryFilter && !$entryFilter($child->getData()));
Copy link
Member

@ycerutoycerutoMar 23, 2017
edited
Loading

Choose a reason for hiding this comment

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

It is about whether the child form/data is empty or not, right?, so the previous nameisEmpty sounds better IMO ;)

@KocKocforce-pushed theform-is-empty-callback branch 3 times, most recently from691cc34 to4c0011eCompareMarch 24, 2017 12:31
$isNew = !isset($previousData[$name]);
$isEmpty = $child->isEmpty() || true === $entryFilter($child->getData());
Copy link
Member

@ycerutoycerutoMar 24, 2017
edited
Loading

Choose a reason for hiding this comment

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

The$entryFilter and the empty closure function looks a bit overkill, can be simplified?

$isEmpty = $child->isEmpty() || (is_callable($this->deleteEmpty) && true === $this->deleteEmpty($child->getData());

HeahDude reacted with thumbs up emoji
Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

changed

));
$this->assertTrue($form->has('0'));
$this->assertFalse($form->has('1'));
$this->assertEquals(array('s_first', 's_last'), $form[0]->getData());
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

fixed

foreach ($form as $name => $child) {
/* @var $child FormInterface */
Copy link
Contributor

Choose a reason for hiding this comment

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

This change should not be done in master.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

why not? it improves autocomplete

Copy link
Contributor

Choose a reason for hiding this comment

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

Because this should target 2.7 :)

@KocKocforce-pushed theform-is-empty-callback branch from4c0011e to3b7f11aCompareApril 9, 2017 21:18
@yceruto
Copy link
Member

Should be rebased to 3.4 branch?

@KocKocforce-pushed theform-is-empty-callback branch from3b7f11a toff51ad8CompareJuly 23, 2017 00:00
@KocKoc changed the base branch frommaster to3.4July 23, 2017 00:00
@KocKocforce-pushed theform-is-empty-callback branch fromff51ad8 to8b49f00CompareJuly 23, 2017 00:03
@Koc
Copy link
ContributorAuthor

Koc commentedJul 23, 2017

@yceruto done
@ogizanagi please review

foreach ($form as $name => $child) {
/* @var $child FormInterface */
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree this should go on a lower branch. Also, It'd rather be:

+            /** @var FormInterface $child */             foreach ($form as $name => $child) {-                 /* @var $child FormInterface */

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

ok, will open PR for that. Where can I find information about standards for inline@var phpdoc?

Copy link
Contributor

Choose a reason for hiding this comment

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

See PSR-5:https://github.com/php-fig/fig-standards/blob/master/proposed/phpdoc.md#3-definitions

About the foreach, though:

This Standard does not cover this specific instance as a foreach statement is not considered to be a "Structural Element" but a Control Flow statement.

but still:

It is RECOMMENDED to precede a "Structural Element" with a DocBlock where it is defined and not with each usage.

Koc reacted with thumbs up emoji
$isNew = !isset($previousData[$name]);
$isEmpty = $child->isEmpty() || (is_callable($entryFilter) && true === $entryFilter($child->getData()));
Copy link
Contributor

Choose a reason for hiding this comment

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

If the callback was set, shouldn't it prevail over (or actually replace entirely) theisEmpty implementation?

- $isEmpty = $child->isEmpty() || (is_callable($entryFilter) && true === $entryFilter($child->getData()));+ $isEmpty = is_callable($entryFilter) ? $entryFilter($child->getData()) : $child->isEmpty();

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

imho not. It will force us to duplicate standard checks inside callables

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not convinced you need those standard checks to be run anyway. For instance, you'll already have to check for the data not being null inside your callable before further checks.

@KocKocforce-pushed theform-is-empty-callback branch from8b49f00 toe697559CompareJuly 23, 2017 09:51
@KocKocforce-pushed theform-is-empty-callback branch frome697559 to8630abeCompareJuly 23, 2017 09:57
@Koc
Copy link
ContributorAuthor

Koc commentedJul 23, 2017

@ogizanagi updated again :), now all green

@@ -148,14 +148,15 @@ public function onSubmit(FormEvent $event)
throw new UnexpectedTypeException($data, 'array or (\Traversable and \ArrayAccess)');
}

if ($this->deleteEmpty) {
$previousData = $event->getForm()->getData();
if ($entryFilter = $this->deleteEmpty) {
Copy link
Member

Choose a reason for hiding this comment

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

I guess$entryFilter can be removed?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Why? This variable uses some lines bottom

Copy link
Member

Choose a reason for hiding this comment

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

Yep, but why not use$this->deleteEmpty directly? IMHO$this->deleteEmpty($child->getData()) is quite easy to understand too.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

because$this->deleteEmpty() will call undefined methodResizeFormListener::deleteEmpty()

yceruto reacted with thumbs up emoji
Copy link
Contributor

@ogizanagiogizanagiJul 24, 2017
edited
Loading

Choose a reason for hiding this comment

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

and($this->deleteEmpty)($child->getData()) is only doable in PHP 7.0+

yceruto reacted with thumbs up emoji
Copy link
Member

Choose a reason for hiding this comment

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

Yep, my bad ;)

@fabpot
Copy link
Member

Thank you@Koc.

@fabpotfabpot merged commit8630abe intosymfony:3.4Jul 26, 2017
@fabpot
Copy link
Member

@Koc Can you submit a pull request to update the docs? Thank you

fabpot added a commit that referenced this pull requestJul 26, 2017
…on. (Koc)This PR was merged into the 3.4 branch.Discussion----------[Form] Allow pass filter callback to delete_empty option.| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | yes| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets |#13601,#13940,#22008,#22014| License       | MIT| Doc PR        | coming soonCommits-------8630abe [Form] Allow pass filter callback to delete_empty option.
This was referencedOct 18, 2017
@KocKoc deleted the form-is-empty-callback branchJuly 22, 2019 12:08
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@ogizanagiogizanagiogizanagi approved these changes

@HeahDudeHeahDudeHeahDude requested changes

@xabbuhxabbuhxabbuh approved these changes

@ycerutoycerutoyceruto approved these changes

Assignees
No one assigned
Projects
None yet
Milestone
3.4
Development

Successfully merging this pull request may close these issues.

9 participants
@Koc@HeahDude@xabbuh@murilolobato@fabpot@yceruto@ogizanagi@nicolas-grekas@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp