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

[FORM] How to create form as service in Symfony 3.* #17013

Closed
ad3n opened this issue Dec 15, 2015 · 18 comments
Closed

[FORM] How to create form as service in Symfony 3.* #17013

ad3n opened this issue Dec 15, 2015 · 18 comments

Comments

@ad3n
Copy link

ad3n commented Dec 15, 2015

Hello all contributors,

How to create form from a service or form type object in Symfony 3.*? Because i check in FormFactory, there is not possible to create form from form type object

@linaori
Copy link
Contributor

linaori commented Dec 15, 2015

Hello @ihsanudin

Can you please use one of the support channels to ask this question instead? Thanks!

@xabbuh xabbuh closed this as completed Dec 15, 2015
@stof
Copy link
Member

stof commented Dec 15, 2015

@ihsanudin defining them as services works exactly the same in Symfony 3 (except you don't need to specify an alias with the form type name in the tag, as there is no name anymore)

@ad3n
Copy link
Author

ad3n commented Dec 15, 2015

@iltar : I was searched before create an issue in here. I found this

http://stackoverflow.com/questions/33451498/symfony-3-define-form-as-a-service

but it's not work properly.

@stof : I was tried like this one

$this->createForm($formTypeObject);

but i got error because createForm need a string not an object

May be you can read my full code in this repo:

https://github.com/symfonyid/adminbundle/blob/master/Controller%2FController.php#L73

@linaori
Copy link
Contributor

linaori commented Dec 15, 2015

@ihsanudin You can also join IRC: Freenode #symfony

@ad3n
Copy link
Author

ad3n commented Dec 15, 2015

Ok, i think, we can't call/create form from form type object

Thanks all i will try another approach

@xabbuh
Copy link
Member

xabbuh commented Dec 15, 2015

@ihsanudin No, that was dropped with Symfony 3.0 as it avoids potentially resolving form types over and over again. So you should simply refer to your form types by name (i.e. the FQCN in Symfony 3).

@stof
Copy link
Member

stof commented Dec 15, 2015

@ihsanudin passing a type object is deprecated in 2.8 and unsupported in 3.0. But this is totally separate from registering types as services

@mvrhov
Copy link

mvrhov commented Dec 15, 2015

@xabbuh:
Registering form types by FQCN is useless. as you are unable to change the form of a 3rdparty bundle.

@ihsanudin: It's a bit different than in sf2.

<service id="k_account.login.form" class="K\Bundle\AccountBundle\Form\Type\LoginType" >
    <factory service="form.factory" method="create" />
    <argument>K\Bundle\AccountBundle\Form\Type\LoginType</argument>
    <argument>null</argument>
    <argument type="collection">
    </argument>
</service>

<service id="k_account.login.form.type" class="K\Bundle\AccountBundle\Form\Type\LoginType">
    <tag name="form.type"/>
    <argument type="service" id="request_stack" />
</service>

@xabbuh
Copy link
Member

xabbuh commented Dec 15, 2015

@mvrhov If you want to customize third party form types, you should use form type extensions. Your service definition is over-complicating as there is no need to create another service using the form factory.

@ad3n
Copy link
Author

ad3n commented Dec 16, 2015

@stof n @xabbuh : 👍 thanks a lot.

I change my form and also my controller and then work properly

https://github.com/symfonyid/adminbundle/blob/master/Form%2FUserType.php

@aliemre
Copy link

aliemre commented Oct 13, 2016

Adding form.type tag is enough in your service definition.

app.form.corporation_type:
        class: App\CorporationBundle\Form\CorporationType
        arguments: ["@doctrine.orm.entity_manager"]
        tags:
            - { name: form.type }

Controller should remain same:

$form = $this->createForm(CorporationType::class, $corporation);

@stof
Copy link
Member

stof commented Oct 13, 2016

creating form themselves as a service is a very bad idea, because the Form object is not reusable (it is a stateful object).

Btw, your service definition is wrong, because the class of your k_account.login.form service will be Symfony\Component\Form\Form, not K\Bundle\AccountBundle\Form\Type\LoginType

@mvrhov
Copy link

mvrhov commented Oct 14, 2016

creating form themselves as a service is a very bad idea, because the Form object is not reusable (it is a stateful object).

We'll I know and that's the way I want them, because the form is also injected into the handler.
And Then inside a controller you simply do.

    public function putOrderAction($id)
    {
        $data = $this->getOrderFromId($id);

        if ($this->handleForm('xxx_store.form.handler.order_edit', $data)) {
            $this->setFlashSuccess('Order information has been saved!');

            return $this
                ->routeRedirectView('store_get_orders', array());
        }

        //this handles common code between editOrderAction & putOrderAction methods 
        return $this->handleEdit($data);
    }

   // this one is in a trait
    protected function handleForm($id, &$data)
    {
        return $this->container->get($id)->process($data);
    }

About the definition: Yeah I have them correctly I was editing things before posting here and it was a copy paste error.

@aliemre
Copy link

aliemre commented Oct 14, 2016

creating form themselves as a service is a very bad idea, because the Form object is not reusable (it is a stateful object).

@stof In case of needing repository data in form type which method should you suggest? As an example, in my form type there are 3 fields that depends on each other sequentially. When I change the "country", it should re-render cities of the country. After the city changed, districts of the city should be re-rendered. To handle it, I need entity manager in form type (by using preSetData & preSubmit listeners). Is there an other convenient way to do it?

@xabbuh
Copy link
Member

xabbuh commented Oct 14, 2016

@aliemre There is nothing wrong in registering your form type as a service (note the difference between the form type and the form that will be created based on the form type by using the form factory).

@aliemre
Copy link

aliemre commented Oct 14, 2016

@stof @xabbuh Sorry for misunderstanding. I got the picture :)

@amine-betari
Copy link

Hi,

I created my form as a service, it works well, by cons I have a problem at the level of modification because I used the inheritance of the forms

An idea please?

@xabbuh
Copy link
Member

xabbuh commented Jan 12, 2017

@amine-betari We do use GitHub only to coordinate the development of Symfony. Please use one of the support channels to seek for help. Thank you for understanding.

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

7 participants