-
- Notifications
You must be signed in to change notification settings - Fork 5.3k
Re-import required getName
function in order to declare form as a… #6728
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
Conversation
… service before 3.x
@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. |
Sure, but the docs says we can declare Form Type as a service without this method, which is wrong. Regarding the form type <?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:
and the related use in controller, of course: $product = new Product(); $form = $this->createForm('product', $product); This is the following error received: I use the Symfony installer with last lts (2.8.8). With the getName() implemented, everything works as expected: what do you think ? |
As I just said above, you should use: $product = new Product(); $form = $this->createForm(ProductType::class, $product); 😄 |
@mickaelandrieu this is because you are missing the But this is deprecated anyway so you should use your form type the new way |
@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 @ogizanagi yes we got it (and we know it exists), thanks for review. |
@mickaelandrieu : I don't see how this is not a 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. |
When have I said that the FQCN is a service ? Form types have always been referenced by their name in Symfony 2.x. In Symfony 2.8+, form type identifiers are their class name rather than a separate name needing to be unique. |
Ok got it now, once and for all ! Thank you @stof :) By the way closed ;) |
It is still required in
2.8
to implement getName() function in order to use a form type as a service.