Skip to content
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

Is it possible define form type in resource, when it is a service? #3843

Closed
voslartomas opened this issue Jan 11, 2016 · 31 comments
Closed

Is it possible define form type in resource, when it is a service? #3843

voslartomas opened this issue Jan 11, 2016 · 31 comments

Comments

@voslartomas
Copy link
Contributor

I have form type, which uses db, so I defined this form as a service and in my controller I am getting this form type via $container->get(). And now I would like to define this form type in resource

sylius_resource:
    resources:
            classes:
                form:
                    default: @service

But it's expecting only string, is there any way to do this?

@pjedrzejewski
Copy link
Member

@ufik You just define a service with same name, so app.form.type.book for example and give it an alias app_book. Sylius will use it from now. :) Please reopen if this does not help. If you have a moment, would be great to add this to the docs, thanks!

@voslartomas
Copy link
Contributor Author

Well I have defined it like this:

services:
    zdg.form.type.registration:
        class: ZDG\EGBundle\Form\Type\RegistrationType
        tags:
            - { name: form.type, alias: zdg_registration }
        arguments: [@doctrine, @sylius.context.locale]

config

sylius_resource:
    resources:
        zdg.registration:
            templates: EGBundle:Backend/Registration
            classes:
                model: ZDG\EGBundle\Entity\Registration

But it still doesn't work. @pjedrzejewski

@voslartomas
Copy link
Contributor Author

Not sure if you have noticed the last message @pjedrzejewski ?

@pjedrzejewski
Copy link
Member

@ufik I did and will try to reproduce it today before tagging v0.16.0. 👍 I will get back to you soon.

@bendavies
Copy link
Contributor

i'm doing this, works for me.

@pjedrzejewski
Copy link
Member

@ufik Are you sure you are loading the definition after SyliusResourceBundle?

@voslartomas
Copy link
Contributor Author

@bendavies Can you please give an example, how do you use this?

@voslartomas
Copy link
Contributor Author

@pjedrzejewski Well you mean order of the bundles, right? We are loading our bundles after bundles from Sylius.

@bendavies
Copy link
Contributor

@ufik looks the same as you, but i use xml

<service id="app.form.type.foo" class="App\Bundle\FooBundle\Form\Type\FooType">
    <argument type="service" id="service.whatever" />
    <tag name="form.type" alias="app_foo" />
</service>
app.foo:
    classes:
        model: App\Bundle\FooBundle\Entity\Foo

@lchrusciel
Copy link
Member

@ufik do you have proper order of resource importing in config file?

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: @SyliusCoreBundle/Resources/config/app/main.yml }
    - { resource: resources.yml }
    - { resource: services.yml }

You should thing about keeping resource definition in a separated yaml file.

@voslartomas
Copy link
Contributor Author

@lchrusciel @pjedrzejewski Well I have configuration of the service in the bundle. And configuration of the resource is almost at the end of the config file...

@lchrusciel
Copy link
Member

@ufik can you show your config file?

@voslartomas
Copy link
Contributor Author

@lchrusciel

imports:
    - { resource: parameters.yml }
    - { resource: zdg/security.yml }
    - { resource: @SyliusCoreBundle/Resources/config/app/main.yml }
    - { resource: @CoreBundle/Resources/config/services.xml }
    - { resource: @CoreBundle/Resources/config/config.yml }
...
...
    - { resource: zdg/sylius_resource.yml }

    - { resource: zdg/eg.yml }

Important is the zdg/eg.yml, where is definition of resources.

sylius_resource:
    resources:
        zdg.registration:
            templates: EGBundle:Backend/Registration
            classes:
                model: ZDG\EGBundle\Entity\Registration

And finally in my bundle services.yml

services:
    zdg.eg.type.registration:
        class: ZDG\EGBundle\Form\Type\RegistrationType
        tags:
            - { name: form.type, alias: zdg_registration }
        arguments: [@doctrine, @sylius.context.locale]

@bendavies
Copy link
Contributor

can you
app/console debug:container | grep form.type
and then
app/console debug:container zdg.eg.type.registration

@lchrusciel
Copy link
Member

@ufik I guess that @CoreBundle/Resources/config/services.xml this line before your service makes trouble. Form types aliases aren't override as far as I know, and first match will win. But it is only guess.

@voslartomas
Copy link
Contributor Author

@lchrusciel Even if I move my config before this configuration, the same.

@voslartomas
Copy link
Contributor Author

@bendavies And first command will not find the service, the second one will find the service. If you meant to have the same grep in pipe, like in the first one:)

@bendavies
Copy link
Contributor

very hard to debug without the code. can you share a fork which replicates the issue?

@voslartomas
Copy link
Contributor Author

I am sorry, but I can't as it is code from our client and the source code is closed :/

@voslartomas
Copy link
Contributor Author

I know it's hard and it's some stupid mistake somewhere in my code, I just can't see that, thanks a lot for all the suggestions, I guess I will need to give it a time and take a look later on.

@bendavies
Copy link
Contributor

yes, but create a new project which replicates the issue

@voslartomas
Copy link
Contributor Author

@bendavies Here is the repo https://github.com/ufik/sylius-1

php app/console sylius:install
php app/console doctrine:fixtures:load

Here is controller with the form as a service, you need to submit form once to see resource in the backend.
http://127.0.0.1:8000/ernaehrung/

then go to
http://127.0.0.1:8000/administration/eg/registrations/1/edit

To see edit form, which should show the same form as on the frontend, but showing default one instead.

You will need to go to the roles and add Manage registrations to the Administrator role.

@bendavies
Copy link
Contributor

Thanks. I'll take a look later

@bendavies
Copy link
Contributor

What are the reproduction steps please?

@voslartomas
Copy link
Contributor Author

Described above...

@bendavies
Copy link
Contributor

@ufik your form type name must match the alias. add to your RegistrationType:

public function getName()
{
    return $this->getBlockPrefix();
}

public function getBlockPrefix()
{
    return 'zdg_registration';
}

@voslartomas
Copy link
Contributor Author

Thank you, such a stupid mistake. Sorry about this guys.

@pjedrzejewski
Copy link
Member

No worries @ufik, this means we need better docs. Thanks!

@bendavies
Copy link
Contributor

@pjedrzejewski actually, normally symfony would tell you this error if you tried to create the form with the form type name, but when sylius uses hasType on the form registry, the error is silently swallowed.
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/FormRegistry.php#L137.

However, in #3762, we don't use useType any more.

@pjedrzejewski
Copy link
Member

@bendavies That's why I suggested registering it under the same service id, this way the generated form is replaced by custom definition and we do not have any error. :)

@bendavies
Copy link
Contributor

Yep! Very good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants