Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[2.7] Expanded Entity Form Element with negative id values #15273

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
Waigie opened this issue Jul 14, 2015 · 2 comments
Closed

[2.7] Expanded Entity Form Element with negative id values #15273

Waigie opened this issue Jul 14, 2015 · 2 comments

Comments

@Waigie
Copy link

Waigie commented Jul 14, 2015

Since 2.7 using expanded Entity Form Elements, which allow multiple selections (checkboxes) with data having negativ IDs is not possible anymore, because the new naming conventions prohibit names starting with a '-'

Example

Database Table

ID Name
-1 Foo
1 Bar
2 Baz

Code

class ExampleForm extends Form
{
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add(
            'element',
            'entity',
            array(
                'class' => 'AppBundle:ExampleEntity',
                'label' => 'Expanded Entity',
                'expanded' => true,
                'multiple' => true,
            )
        );
    }
}

Creating this form with the Symfony Form Builder will generate the following Error:

The name "-1" contains illegal characters. Names should start with a letter, digit or underscore and only contain letters, digits, numbers, underscores ("_"), hyphens ("-") and colons (":").

Prior to Symfony 2.7.0 everything works fine, including 2.6.10.

@jakzal
Copy link
Contributor

jakzal commented Jul 14, 2015

Reproduced here.

@jakzal
Copy link
Contributor

jakzal commented Jul 14, 2015

re #14050

@Waigie until this is fixed, you can implement a workaround and define a choice_name callback:

        $builder->add(
            'element',
            'entity',
            [
                'class' => ExampleEntity::class,
                'label' => 'Expanded Entity',
                'expanded' => true,
                'multiple' => true,
                'choice_name' => function ($choice) {
                    return str_replace('-', '_', $choice);
                }
            ]
        );

fabpot added a commit that referenced this issue Jul 15, 2015
This PR was merged into the 2.7 branch.

Discussion
----------

[Form] Fix a BC break in the entity

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #15273
| License       | MIT
| Doc PR        | -

@webmozart this brings back the old behaviour. Attached test case passes on 2.3 with no code modifications.

Code [here](https://github.com/symfony/symfony/blob/73bbaa6cc7d706b0e945107657fbd240efec0c97/src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php#L199-L206) promises any integer number can be handled with `createChoiceName()`:

```php
            // If the object has a single-column, numeric ID, use that ID as
            // field name. We can only use numeric IDs as names, as we cannot
            // guarantee that a non-numeric ID contains a valid form name
            if ($idReader->isIntId()) {
                return array(__CLASS__, 'createChoiceName');
            }
            // Otherwise, an incrementing integer is used as name automatically
```

While `createChoiceName()` can only handle natural numbers.

Commits
-------

03642b8 [Form] Fix a BC break in the entity
@fabpot fabpot closed this as completed Jul 15, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants