8000 [Form] FormInterface::getPropertyPath() return type incosistency · Issue #24560 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] FormInterface::getPropertyPath() return type incosistency #24560

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
vudaltsov opened this issue Oct 14, 2017 · 2 comments
Closed

[Form] FormInterface::getPropertyPath() return type incosistency #24560

vudaltsov opened this issue Oct 14, 2017 · 2 comments

Comments

@vudaltsov
Copy link
Contributor
vudaltsov commented Oct 14, 2017
Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 3

In FormInterface::getPropertyPath() the return type is not nullable.

But its implementation in Form::getPropertyPath() actually returns null. This costed me a Call to a member function getElements() on null since I was relying on IDE autocomplete.

@vudaltsov vudaltsov changed the title [Form] Return type incosistency [Form] FormInterface::getPropertyPath() return type incosistency Oct 14, 2017
@Simperfit
Copy link
Contributor

Should we update the interface, or maybe change the behaviour when retuning null WDYT ?

@vudaltsov
Copy link
Contributor Author

null is returned when the form has an empty name. It allows for unprefixed children.

<?php

namespace App\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\TextType;

class IndexController extends AbstractController
{
    /**
     * @Route(name="index")
     * @Template()
     */
    public function indexAction()
    {
        $form = $this->get('form.factory')
            ->createNamedBuilder('')
            ->add('text', TextType::class)
            ->getForm();

        return [
            'form' => $form->createView(),
        ];
    }
}
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Welcome!</title>
    </head>
    <body>
        <form name="" method="post">
            <label for="text">Text</label>
            <input type="text" id="text" name="text">
        </form>
    </body>
</html>

We cannot change this behaviour, obviously.

fabpot added a commit that referenced this issue Nov 5, 2017
…sov)

This PR was merged into the 2.7 branch.

Discussion
----------

[Form] Nullable FormInterface::getPropertyPath()

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

`Symfony\Component\Form\Form::getPropertyPath()` returns `null` when the form has an empty name. It allows for unprefixed children.

```php
<?php

namespace App\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\TextType;

class IndexController extends AbstractController
{
    /**
     * @route(name="index")
     * @template()
     */
    public function indexAction()
    {
        $form = $this->get('form.factory')
            ->createNamedBuilder('')
            ->add('text', TextType::class)
            ->getForm();

        return [
            'form' => $form->createView(),
        ];
    }
}
```

```html
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Welcome!</title>
    </head>
    <body>
        <form name="" method="post">
            <label for="text">Text</label>
            <input type="text" id="text" name="text">
        </form>
    </body>
</html>
```

But the return type of the `Symfony\Component\Form\FormInterface::getPropertyPath()` is not nullable.

We cannot change the behaviour, obviously. At least it's useful in API controllers.

So I decided to change the doc block of the interface.

Commits
-------

d56632a FormInterface::getPropertyPath(): PropertyPathInterface|null
@fabpot fabpot closed this as completed Nov 5, 2017
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

5 participants
0