{"id":794,"date":"2016-07-14T18:08:11","date_gmt":"2016-07-15T01:08:11","guid":{"rendered":"http:\/\/www.keganv.com\/?p=794"},"modified":"2021-01-26T18:33:57","modified_gmt":"2021-01-27T01:33:57","slug":"passing-arguments-controller-file-type-symfony-3","status":"publish","type":"post","link":"https:\/\/www.keganv.com\/passing-arguments-controller-file-type-symfony-3\/","title":{"rendered":"Passing Arguments From Controller To Form Type In Symfony 3"},"content":{"rendered":"<p>This week I have been updating my <a href=\"https:\/\/www.keganv.com\/prayfornews\/\" target=\"_blank\" rel=\"noopener\">Pray For News app<\/a> that I built for my church, and other churches and groups to use. The app is built in Symfony2 and currently is in version 2.7. I am updating the framework to Symfony 3.1 and removing all the deprecations. Most deprecation notices are easy to eliminate and deal with forms.<br \/>\n<!--more--><\/p>\n<p>If you are updating your app and you find a deprecation notice that says:<br \/>\n<code>Passing type instances to FormBuilder::add(), Form::add() or the FormFactory is deprecated since version 2.8 and will not be supported in 3.0. Use the fully-qualified type class name instead.<\/code><\/p>\n<p>Likely you are instantiating a new instance of your form like <code>new YourFormType()<\/code>. This was fine up until version 2.8 and is no longer the way to get an instance of your form. Unless you <a href=\"http:\/\/symfony.com\/doc\/current\/best_practices\/forms.html#registering-forms-as-services\" target=\"_blank\" rel=\"noopener\">make your form into a service<\/a>, which many times I think is a good idea because you can easily inject dependencies.<\/p>\n<h4>The deprecated way of instantiating a form<\/h4>\n<p>In my case, I was passing dependency arguments to the form&#8217;s <code>__construct<\/code> method from my controller. Below is an example of the old way to do things.<\/p>\n<p>The controller:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$form = $this-&gt;createForm(new TeamType($currentOrg), $entity, array(\r\n    'action' =&gt; $this-&gt;generateUrl('teams_update', array('id' =&gt; $entity-&gt;getId())),\r\n    'method' =&gt; 'PUT',\r\n));\r\n<\/pre>\n<p>The form type:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nclass TeamType extends AbstractType\r\n{\r\n    private $organization;\r\n\r\n    public function __construct(Organization $currentOrg)\r\n    {\r\n        $this-&gt;organization = $currentOrg;\r\n    }\r\n    ...\r\n}\r\n<\/pre>\n<h4>The updated way of instantiating a form<\/h4>\n<p>The new and correct way of getting an instance of your form since Symfony 2.8 is to pass the fully qualified name of the form&#8217;s class. For example, pass <code>FormType::class<\/code> or <code>Namespace\\YourAppBundle\\Form\\YourFormType<\/code> to the <code>createForm<\/code> method.<\/p>\n<p>You can see the problem if you wanted to inject dependencies to the construct. In my case, I needed to send the user&#8217;s current organization entity that they are logged in under.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$form = $this-&gt;createForm(TeamType::class, $entity, array(\r\n    'action'     =&gt; $this-&gt;generateUrl('teams_update', array('id' =&gt; $entity-&gt;getId())),\r\n    'method'     =&gt; 'PUT',\r\n    'currentOrg' =&gt; $currentOrg-&gt;getId(),\r\n));\r\n<\/pre>\n<p>So what to do when you can no longer pass dependencies to the construct? You can pass them in the array options to the <code>buildForm<\/code> method! Do this in the third argument of the <code>createForm<\/code> method in the controller, like the example above.<\/p>\n<p>Then in your form type, you can grab the arguments, like below.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/**\r\n * @param FormBuilderInterface $builder\r\n * @param array $options\r\n *\/\r\npublic function buildForm(FormBuilderInterface $builder, array $options)\r\n{\r\n    $this-&gt;currentOrg = $options&#x5B;'currentOrg'];\r\n    ...\r\n}\r\n<\/pre>\n<p>You also must declare your options in the <code>setDefaults<\/code> method of the <code>configureOptions<\/code> function. Be sure that you are using the new <code>OptionsResolver<\/code> class and not the old <code>OptionsResolverInterface<\/code> class. For good measure I am also setting the setRequired method and the setTypes() method as well.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/**\r\n * @param OptionsResolver $resolver\r\n *\/\r\npublic function configureOptions(OptionsResolver $resolver)\r\n{\r\n    $resolver-&gt;setDefaults(&#x5B;\r\n            'data_class' =&gt; 'Namespace\\AppBundle\\Entity\\Team',\r\n            'currentOrg' =&gt; 1,\r\n    ]);\r\n\r\n    $resolver-&gt;setRequired('currentOrg'); \/\/ Requires that currentOrg be set by the caller.\r\n    $resolver-&gt;setAllowedTypes('currentOrg', 'integer'); \/\/ Validates the type(s) of option(s) passed.\r\n}\r\n<\/pre>\n<p>Easy enough, right? A special thanks to <a href=\"http:\/\/stackoverflow.com\/questions\/34027711\/passing-data-to-buildform-in-symfony-2-8-3-0\" target=\"_blank\" rel=\"noopener\">this StackOverflow question<\/a> for reference. Leave your comments and any additional knowledge or tricks you have for Symfony 3 forms below!<\/p>\n","protected":false},"excerpt":{"rendered":"This week I have been updating my Pray For News app that I built for my church, and other churches and groups to use. The app is built in Symfony2 and currently is in version 2.7. I am updating the framework to Symfony 3.1 and removing all the deprecations. Most deprecation notices are easy to &#8230; <br\/> <a class=\"view-article btn btn-primary flip pull-left\" href=\"https:\/\/www.keganv.com\/passing-arguments-controller-file-type-symfony-3\/\"><span>View Article<\/span><\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-794","post","type-post","status-publish","format-standard","hentry","category-coding-programming"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Passing Arguments From Controller To Form Type In Symfony 3 | KeganV<\/title>\n<meta name=\"description\" content=\"Learn how to pass arguments and variables from a controller to form types in Symfony 2.8 - 3.0.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.keganv.com\/passing-arguments-controller-file-type-symfony-3\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Passing Arguments From Controller To Form Type In Symfony 3 | KeganV\" \/>\n<meta property=\"og:description\" content=\"Learn how to pass arguments and variables from a controller to form types in Symfony 2.8 - 3.0.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.keganv.com\/passing-arguments-controller-file-type-symfony-3\/\" \/>\n<meta property=\"og:site_name\" content=\"KeganV\" \/>\n<meta property=\"article:published_time\" content=\"2016-07-15T01:08:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-01-27T01:33:57+00:00\" \/>\n<meta name=\"author\" content=\"Kegan V.\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kegan V.\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.keganv.com\/passing-arguments-controller-file-type-symfony-3\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.keganv.com\/passing-arguments-controller-file-type-symfony-3\/\"},\"author\":{\"name\":\"Kegan V.\",\"@id\":\"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276\"},\"headline\":\"Passing Arguments From Controller To Form Type In Symfony 3\",\"datePublished\":\"2016-07-15T01:08:11+00:00\",\"dateModified\":\"2021-01-27T01:33:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.keganv.com\/passing-arguments-controller-file-type-symfony-3\/\"},\"wordCount\":507,\"commentCount\":8,\"publisher\":{\"@id\":\"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276\"},\"articleSection\":[\"Coding &amp; Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.keganv.com\/passing-arguments-controller-file-type-symfony-3\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.keganv.com\/passing-arguments-controller-file-type-symfony-3\/\",\"url\":\"https:\/\/www.keganv.com\/passing-arguments-controller-file-type-symfony-3\/\",\"name\":\"Passing Arguments From Controller To Form Type In Symfony 3 | KeganV\",\"isPartOf\":{\"@id\":\"https:\/\/www.keganv.com\/#website\"},\"datePublished\":\"2016-07-15T01:08:11+00:00\",\"dateModified\":\"2021-01-27T01:33:57+00:00\",\"description\":\"Learn how to pass arguments and variables from a controller to form types in Symfony 2.8 - 3.0.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.keganv.com\/passing-arguments-controller-file-type-symfony-3\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.keganv.com\/passing-arguments-controller-file-type-symfony-3\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.keganv.com\/passing-arguments-controller-file-type-symfony-3\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.keganv.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Passing Arguments From Controller To Form Type In Symfony 3\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.keganv.com\/#website\",\"url\":\"https:\/\/www.keganv.com\/\",\"name\":\"KeganV\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.keganv.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276\",\"name\":\"Kegan V.\",\"logo\":{\"@id\":\"https:\/\/www.keganv.com\/#\/schema\/person\/image\/\"},\"sameAs\":[\"http:\/\/www.keganv.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Passing Arguments From Controller To Form Type In Symfony 3 | KeganV","description":"Learn how to pass arguments and variables from a controller to form types in Symfony 2.8 - 3.0.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.keganv.com\/passing-arguments-controller-file-type-symfony-3\/","og_locale":"en_US","og_type":"article","og_title":"Passing Arguments From Controller To Form Type In Symfony 3 | KeganV","og_description":"Learn how to pass arguments and variables from a controller to form types in Symfony 2.8 - 3.0.","og_url":"https:\/\/www.keganv.com\/passing-arguments-controller-file-type-symfony-3\/","og_site_name":"KeganV","article_published_time":"2016-07-15T01:08:11+00:00","article_modified_time":"2021-01-27T01:33:57+00:00","author":"Kegan V.","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kegan V.","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.keganv.com\/passing-arguments-controller-file-type-symfony-3\/#article","isPartOf":{"@id":"https:\/\/www.keganv.com\/passing-arguments-controller-file-type-symfony-3\/"},"author":{"name":"Kegan V.","@id":"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276"},"headline":"Passing Arguments From Controller To Form Type In Symfony 3","datePublished":"2016-07-15T01:08:11+00:00","dateModified":"2021-01-27T01:33:57+00:00","mainEntityOfPage":{"@id":"https:\/\/www.keganv.com\/passing-arguments-controller-file-type-symfony-3\/"},"wordCount":507,"commentCount":8,"publisher":{"@id":"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276"},"articleSection":["Coding &amp; Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.keganv.com\/passing-arguments-controller-file-type-symfony-3\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.keganv.com\/passing-arguments-controller-file-type-symfony-3\/","url":"https:\/\/www.keganv.com\/passing-arguments-controller-file-type-symfony-3\/","name":"Passing Arguments From Controller To Form Type In Symfony 3 | KeganV","isPartOf":{"@id":"https:\/\/www.keganv.com\/#website"},"datePublished":"2016-07-15T01:08:11+00:00","dateModified":"2021-01-27T01:33:57+00:00","description":"Learn how to pass arguments and variables from a controller to form types in Symfony 2.8 - 3.0.","breadcrumb":{"@id":"https:\/\/www.keganv.com\/passing-arguments-controller-file-type-symfony-3\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.keganv.com\/passing-arguments-controller-file-type-symfony-3\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.keganv.com\/passing-arguments-controller-file-type-symfony-3\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.keganv.com\/"},{"@type":"ListItem","position":2,"name":"Passing Arguments From Controller To Form Type In Symfony 3"}]},{"@type":"WebSite","@id":"https:\/\/www.keganv.com\/#website","url":"https:\/\/www.keganv.com\/","name":"KeganV","description":"","publisher":{"@id":"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.keganv.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276","name":"Kegan V.","logo":{"@id":"https:\/\/www.keganv.com\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/www.keganv.com"]}]}},"_links":{"self":[{"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/posts\/794"}],"collection":[{"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/comments?post=794"}],"version-history":[{"count":26,"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/posts\/794\/revisions"}],"predecessor-version":[{"id":1407,"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/posts\/794\/revisions\/1407"}],"wp:attachment":[{"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/media?parent=794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/categories?post=794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/tags?post=794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}