Skip to content

Conversation

mickaelandrieu
Copy link
Contributor

It is still required in 2.8 to implement getName() function in order to use a form type as a service.

@ogizanagi
Copy link
Contributor

ogizanagi commented Jul 7, 2016

@mickaelandrieu : ❓ It isn't required at all in 2.8 (except for third-party bundles that needs to support sf < 2.8 versions, so for BC reasons).

Simply use the form type FQCN.

@mickaelandrieu
Copy link
Contributor Author

mickaelandrieu commented Jul 7, 2016

Simply use the form type FQCN.

Sure, but the docs says we can declare Form Type as a service without this method, which is wrong.

Regarding the form type ProductType:

<?php namespace AppBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\NumberType; use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\DateTimeType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; class ProductType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('name') ->add('price', NumberType::class) ->add('description', TextareaType::class) ->add('created_at', DateTimeType::class) ->add('is_online', ChoiceType::class, [ 'choices' => [ 'Yes' => true, 'No' => false, ], 'choices_as_values' => true, // required before 3.x ]) ->add('save', SubmitType::class) ; } /* this method still need to be implement if you want to use the service name */ /*public function getName()  {  return 'product';  }*/ }

With the following service declaration:

# app/config/services.yml services: product: class: AppBundle\Form\Type\ProductType tags: - { name: form.type } 

and the related use in controller, of course:

$product = new Product(); $form = $this->createForm('product', $product);

This is the following error received:

without_getname

I use the Symfony installer with last lts (2.8.8).

With the getName() implemented, everything works as expected: what do you think ?

@ogizanagi
Copy link
Contributor

As I just said above, you should use:

$product = new Product(); $form = $this->createForm(ProductType::class, $product);

😄

@stof
Copy link
Member

stof commented Jul 7, 2016

@mickaelandrieu this is because you are missing the alias in the tag in the service definition to register the type for the legacy way too, and so it cannot be found through its legacy name. Just defining the method will not make it magically work.

But this is deprecated anyway so you should use your form type the new way

@mickaelandrieu
Copy link
Contributor Author

@stof even if it's a new "best practice" to not use a "Form as a service" notation, we can't say that FQCN is a service :D

We should probably remove the Forms as a service section from Form component if it's not designed to work anymore .. what do you think @stof ? I can update my pull request.

@ogizanagi yes we got it (and we know it exists), thanks for review.

@ogizanagi
Copy link
Contributor

ogizanagi commented Jul 7, 2016

@mickaelandrieu : I don't see how this is not a Form as a service considering the fact you declared the form in the DIC configuration as before, except you don't need to set the getName method nor the alias attribute anymore !

Either I totally not understand you, either you truly think using a form type as a service is not available anymore with Symfony in 3.0 and above.

What is explained in http://symfony.com/doc/2.8/book/forms.html#defining-your-forms-as-services is perfectly valid, and there is no reason for removing this section IMHO.

@stof
Copy link
Member

stof commented Jul 7, 2016

When have I said that the FQCN is a service ?

Form types have always been referenced by their name in Symfony 2.x. product in your call to createForm('product') is not the service id (except that there was a legacy feature which was using the service id to replace the alias when the tag does not provide the alias explicitly, which is why things work in your case even if you don't provide the alias explicitly).

In Symfony 2.8+, form type identifiers are their class name rather than a separate name needing to be unique.
Form types can still be registered as service if you want (this is necessary when the form type needs some dependencies). However it is useless for form types having no dependencies because the registry can perform on-demand registration of the form type (as the identifier is the class name, it knows how to instantiate the type when it does not know it yet).
Defining form types as services will not be removed, as having form types with some dependencies is a legitimate use case.

@mickaelandrieu
Copy link
Contributor Author

except that there was a legacy feature which was using the service id to replace the alias when the tag does not provide the alias explicitly, which is why things work in your case even if you don't provide the alias explicitly

Ok got it now, once and for all ! Thank you @stof :)

By the way closed ;)

@mickaelandrieu mickaelandrieu deleted the patch-4 branch July 7, 2016 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

4 participants