8000 [Form] Mix attr option between guessed options and user options by yceruto · Pull Request #22936 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Mix attr option between guessed options and user options #22936

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
Jun 3, 2017

Conversation

yceruto
Copy link
Member
@yceruto yceruto commented May 28, 2017
Q A
Branch? 2.7
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #19871
License MIT

@@ -126,7 +126,7 @@ public function createBuilderForProperty($class, $property, $data = null, array

// user options may override guessed options
if ($typeGuess) {
$options = array_merge($typeGuess->getOptions(), $options);
$options = array_replace_recursive($typeGuess->getOptions(), $options);
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure if it is a good idea to apply this "globally". For the attr option, doing a recursive merge does work. But this may not be the case for all other options where the usage of some keys may be mutually exclusive.

Copy link
Member

Choose a reason for hiding this comment

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

What we can do: First merge all guess options recursively and then do the merge here as before.

Copy link
Member Author
@yceruto yceruto May 29, 2017

Choose a reason for hiding this comment

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

But this may not be the case for all other options where the usage of some keys may be mutually exclusive.

Sorry :/ I don't see what I missing here, would you show me an example, please? I've done a simple test (https://3v4l.org/5FvnM) but I don't see what is wrong with array_replace_recursive() function :/

Copy link
Member

Choose a reason for hiding this comment

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

see https://3v4l.org/JOK0d

We don't know if it is allowed to use both keys baz and bar at the same time for the foo option.

Copy link
Member
@xabbuh xabbuh May 29, 2017

Choose a reason for hiding this comment

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

I suggest to change the code as follows:

$guessedOptions = array();

if (null !== $pattern) {
    $guessedOptions = array_replace_recursive(array('attr' => array('pattern' => $pattern)), $guessedOptions);
}

if (null !== $maxLength) {
    $guessedOptions = array_replace_recursive(array('attr' => array('maxlength' => $maxLength)), $guessedOptions);
}

if ($requiredGuess) {
    $guessedOptions = array_merge(array('required' => $requiredGuess->getValue()), $guessedOptions);
}

if ($typeGuess) {
    $guessedOptions = array_replace_recursive($typeGuess->getOptions(), $guessedOptions);
}

// user options may override guessed options
$options = array_merge($guessedOptions, $options);

Copy link
Member Author
@yceruto yceruto May 29, 2017

Choose a reason for hiding this comment

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

Following the first assumption:

But this may not be the case for all other options where the usage of some keys may be mutually exclusive.

Still we can pass false to the user option and it not will be displayed, i.e.:

$form->add('foo', null, array('foo' => array('baz' => false, 'bar' => 'foobar')));

//...

$customGuessedOptions = array('foo' => array('baz' => 'foobaz'));

Copy link
Member

Choose a reason for hiding this comment

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

My comment above should have used array_replace_recursive() for the type guess too (I updated it).

Copy link
Member

Choose a reason for hiding this comment

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

Losing the maxlength attribute when passing the attr option is expected IMO as you opted to be more clever than the guessers for the attr option.

Copy link
Member Author
@yceruto yceruto May 29, 2017

Choose a reason for hiding this comment

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

Losing the maxlength attribute when passing the attr option is expected IMO [...]

It's not a BC break? before this PR it's not true and the issue description proposes mix attr options also between guessed options and populated options, so now the one that is lost is me :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Losing the maxlength attribute when passing the attr option is expected

It should not as it could trigger an unwanted deprecation.

@nicolas-grekas nicolas-grekas added this to the 2.7 milestone May 29, 2017
@yceruto yceruto changed the title [Form] Override options recursively [Form] Mix attr option between guessed options and user options May 29, 2017
@yceruto
Copy link
Member Author
yceruto commented May 29, 2017

I've updated the solution mixing attr options between type-guess options and user options. It's already supported between maxlength/pattern and user options attributes.

Status: Needs Review

@fabpot
Copy link
Member
fabpot commented Jun 3, 2017

Thank you @yceruto.

@fabpot fabpot merged commit 84f5de9 into symfony:2.7 Jun 3, 2017
fabpot added a commit that referenced this pull request Jun 3, 2017
…tions (yceruto)

This PR was merged into the 2.7 branch.

Discussion
----------

[Form] Mix attr option between guessed options and user options

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #19871
| License       | MIT

Commits
-------

84f5de9 mix attr options between type-guess options and user options
@yceruto yceruto deleted the fix_19871 branch June 4, 2017 18:56
This was referenced Jun 6, 2017
@fabpot fabpot mentioned this pull request Jul 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
0