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

Skip to content

[Form] Add default implementation for AbstractType::getName() #15008

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
webmozart opened this issue Jun 16, 2015 · 4 comments
Closed

[Form] Add default implementation for AbstractType::getName() #15008

webmozart opened this issue Jun 16, 2015 · 4 comments
Labels
DX DX = Developer eXperience (anything that improves the experience of using Symfony) Enhancement Form

Comments

@webmozart
Copy link
Contributor

As originally discussed in #11185, a default implementation should be added for AbstractType::getName():

  • getName() should return the type's FQCN by default
  • the alias should be se4t to the class name by default in the DI config
  • the form name should be set to the lower-cased short name of the type by default, minus the "type" suffix

Before:

class TaskType extends AbstractType
{
    public function getName()
    {
        return 'task';
    }
}
services:
    acme.demo.form.task:
        class: Acme\DemoBundle\Form\TaskType
        tags:
            - { name: form.type, alias: task }
$form = $this->createForm(new TaskType());
// or
$form = $this->createForm('task');

echo $form->getName();
// task

After:

class TaskType extends AbstractType
{
}
services:
    acme.demo.form.task:
        class: Acme\DemoBundle\Form\TaskType
        tags:
            - { name: form.type }
$form = $this->createForm(new TaskType());
// or
$form = $this->createForm(TaskType::class);
// or
$form = $this->createForm('Acme\DemoBundle\Form\TaskType');

echo $form->getName();
// task
@webmozart webmozart added Form DX DX = Developer eXperience (anything that improves the experience of using Symfony) Enhancement labels Jun 16, 2015
@linaori
Copy link
Contributor

linaori commented Jun 16, 2015

I think this is a nice idea for services, but what about multiple services that automatically get the same alias? \Foo\BarType and \Baz\BarType would create an automatic conflict and could create problems between 3rd party bundles.

@stof
Copy link
Member

stof commented Jun 16, 2015

form names don't need to be unique (unless you try to submit them in the same request, which would be really weird). And type names will not conflict by default as the default value would be the FQCN

@linaori
Copy link
Contributor

linaori commented Jun 16, 2015

@stof so the Alias would default to TaskType::class? That would make sense. I Misunderstood it and thought the idea was to put task as alias, but task will only be the name.

@webmozart
Copy link
Contributor Author

Implemented in #15079.

fabpot added a commit that referenced this issue Aug 1, 2015
…sing of type instances (webmozart)

This PR was merged into the 2.8 branch.

Discussion
----------

[Form] Deprecated FormTypeInterface::getName() and passing of type instances

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

#### Type Names

This PR deprecates the definition of the `getName()` method of form types. See #15008 for a more detailed description.

Before:

```php
class MyType extends AbstractType
{
    public function getName()
    {
        return 'mytype';
    }

    // ...
}
```

After:

```php
class MyType extends AbstractType
{
    // ...
}
```

You should always reference other types by their fully-qualified class names. Thanks to PHP 5.5, that's easy:

Before:

```php
$form = $this->createFormBuilder()
    ->add('name', 'text')
    ->add('age', 'integer')
    ->getForm();
```

After:

```php
$form = $this->createFormBuilder()
    ->add('name', TextType::class)
    ->add('age', IntegerType::class)
    ->getForm();
```

#### Type Instances

Furthermore, passing of type instances is deprecated.

Before:

```php
$form = $this->createForm(new AuthorType());
```

After:

```php
$form = $this->createForm(AuthorType::class);
```

#### DIC Aliases

When registering a type in the DIC, you should omit the "alias" attribute now.

Before:

```xml
<service id="my.type" class="Vendor\Type\MyType">
    <tag name="form.type" alias="mytype" />
    <argument type="service" id="some.service.id" />
</service>
```

After:

```xml
<service id="my.type" class="Vendor\Type\MyType">
    <tag name="form.type" />
    <argument type="service" id="some.service.id" />
</service>
```

Types without dependencies don't need to be registered in the DIC as they can be instantiated right away.

#### Template Block Prefixes

By default, the class name of the type in underscore notation minus "Type" suffix is used as Twig template block prefix (e.g. `UserProfileType` => `user_profile_*`). If you want to customize that, overwrite the new `getBlockPrefix()` method in your type:

```php
class UserProfileType extends AbstractType
{
    public function getBlockPrefix()
    {
        return 'profile';
    }

    // ...
}
```

Commits
-------

3d9e5de [Form] Deprecated FormTypeInterface::getName() and passing of type instances
@fabpot fabpot closed this as completed Aug 1, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DX DX = Developer eXperience (anything that improves the experience of using Symfony) Enhancement Form
Projects
None yet
Development

No branches or pull requests

4 participants