-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Comments
Hello @ihsanudin Can you please use one of the support channels to ask this question instead? Thanks! |
@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) |
@iltar : I was searched before create an issue in here. I found this
but it's not work properly. @stof : I was tried like this one
but i got error because May be you can read my full code in this repo:
|
@ihsanudin You can also join IRC: Freenode #symfony |
Ok, i think, we can't call/create form from form type object Thanks all i will try another approach |
@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). |
@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 |
@xabbuh: @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> |
@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. |
Adding form.type tag is enough in your service definition.
Controller should remain same:
|
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 |
We'll I know and that's the way I want them, because the form is also injected into the handler. 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. |
@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? |
@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). |
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? |
@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. |
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
The text was updated successfully, but these errors were encountered: