{"id":24377713,"url":"https://github.com/codete/formgeneratorbundle","last_synced_at":"2025-04-10T19:51:37.042Z","repository":{"id":56955766,"uuid":"30537690","full_name":"codete/FormGeneratorBundle","owner":"codete","description":"Symfony Bundle for dynamic Form generation","archived":false,"fork":false,"pushed_at":"2017-12-06T15:54:12.000Z","size":84,"stargazers_count":34,"open_issues_count":2,"forks_count":7,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-03-24T17:49:56.656Z","etag":null,"topics":["bundle","php","symfony","symfony-bundle"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codete.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-02-09T13:44:58.000Z","updated_at":"2024-11-30T18:00:07.000Z","dependencies_parsed_at":"2022-08-21T08:50:29.808Z","dependency_job_id":null,"html_url":"https://github.com/codete/FormGeneratorBundle","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codete%2FFormGeneratorBundle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codete%2FFormGeneratorBundle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codete%2FFormGeneratorBundle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codete%2FFormGeneratorBundle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codete","download_url":"https://codeload.github.com/codete/FormGeneratorBundle/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248283721,"owners_count":21077910,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bundle","php","symfony","symfony-bundle"],"created_at":"2025-01-19T06:16:46.610Z","updated_at":"2025-04-10T19:51:37.021Z","avatar_url":"https://github.com/codete.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"FormGeneratorBundle\n===================\n\n[![Build Status](https://travis-ci.org/codete/FormGeneratorBundle.svg?branch=master)](https://travis-ci.org/codete/FormGeneratorBundle)\n[![SensioLabsInsight](https://insight.sensiolabs.com/projects/8893e0c9-ed68-498e-aa86-63320ac43a62/mini.png)](https://insight.sensiolabs.com/projects/8893e0c9-ed68-498e-aa86-63320ac43a62)\n\nWe were extremely bored with writing/generating/keeping-up-to-date\nour FormType classes so we wanted to automate the process and limit\nrequired changes only to Entity/Document/Whatever class and get new\nform out of the box - this is how FormGenerator was invented.\n\n**You're looking at the documentation for version 2.0**\n\n- [go to 1.x documentation](https://github.com/codete/FormGeneratorBundle/blob/1.3.0/README.md)\n- [see UPGRADE.md for help with upgrading](https://github.com/codete/FormGeneratorBundle/blob/master/UPGRADE-2.0.md)\n\nBasic Usages\n------------\n\nConsider a class\n\n``` php\nuse Codete\\FormGeneratorBundle\\Annotations as Form;\n// import Symfony form types so ::class will work\n\n/**\n * @Form\\Form(\n *  personal = { \"title\", \"name\", \"surname\", \"photo\", \"active\" },\n *  work = { \"salary\" },\n *  admin = { \"id\" = { \"type\" = NumberType::class }, \"surname\" }\n * )\n */\nclass Person\n{\n    public $id;\n    \n    /**\n     * @Form\\Field(type=ChoiceType::class, choices = { \"Mr.\" = \"mr\", \"Ms.\" = \"ms\" })\n     */\n    public $title;\n    \n    /**\n     * @Form\\Field(type=TextType::class)\n     */\n    public $name;\n    \n    /**\n     * @Form\\Field(type=TextType::class)\n     */\n    public $surname;\n    \n    /**\n     * @Form\\Field(type=FileType::class)\n     */\n    public $photo;\n    \n    /**\n     * @Form\\Field(type=CheckboxType::class)\n     */\n    public $active;\n    \n    /**\n     * @Form\\Field(type=MoneyType::class)\n     */\n    public $salary;\n}\n```\n\nNow instead of writing whole ``PersonFormType`` and populating\nFormBuilder there we can use instead:\n\n``` php\nuse Codete\\FormGeneratorBundle\\FormGenerator;\n\n$generator = $this-\u003eget(FormGenerator::class);\n\n$person = new Person();\n$form = $generator-\u003ecreateFormBuilder($person)-\u003egetForm();\n$form-\u003ehandleRequest($request);\n```\n\nVoila! Form for editing all annotated properties is generated for us.\nWe could even omit ``type=\"..\"`` in annotations if Symfony will be\nable to guess the field's type for us.\n\nSpecifying Field Options\n------------------------\n\nBy default everything you specify in `@Form\\Field` (except for `type`) annotation\nwill be passed as an option to generated form type. To illustrate:\n\n```php\n/**\n * @Form\\Field(type=ChoiceType::class, choices = { \"Mr.\" = \"mr\", \"Ms.\" = \"ms\" }, \"attr\" = { \"class\" = \"foo\" })\n */\npublic $title;\n```\n\nis equivalent to:\n\n```php\n$fb-\u003eadd('title', ChoiceType::class, [\n    'choices' =\u003e [ 'Mr.' =\u003e 'mr', 'Ms.' =\u003e 'ms' ],\n    'attr' =\u003e [ 'class' =\u003e 'foo' ],\n]);\n```\n\nThis approach has few advantages like saving you a bunch of keystrokes each time you\nare specifying options, but there are downsides too. First, if you have any custom\noption for one of your modifiers you forget to `unset`, Symfony will be unhappy and\nwill let you know by throwing an exception. Another downside is that we have reserved\n`type` property and it's needed as an option for the repeated type. If you ever find\nyourself in one of described cases, or you just prefer to be explicit, you can put\nall Symfony fields' options into an `options` property:\n\n```php\n/**\n * @Form\\Field(\n *   type=ChoiceType::class,\n *   options={ \"choices\" = { \"Mr.\" = \"mr\", \"Ms.\" = \"ms\" }, \"attr\" = { \"class\" = \"foo\" } }\n * )\n */\npublic $title;\n```\n\nWhen Form Generator creates a form field and finds `options` property, it will pass\nthem as that field's options to the `FormBuilder`. Effectively this allows you to\nseparate field's options from options for your configuration modifiers which can be\na gain on its own.\n\nAdding fields not mapped to a property\n--------------------------------------\n\nSometimes you may need to add a field that will not be mapped to a property. An example\nof such use case is adding buttons to the form:\n\n```php\n/**\n * The first value in Field annotation specifies field's name.\n *\n * @Form\\Field(\"reset\", type=ResetType::class)\n * @Form\\Field(\"submit\", type=SubmitType::class, \"label\"=\"Save\")\n */\nclass Person\n```\n\nAll fields added on the class level come last in the generated form, unless a form view \n(described below) specifies otherwise. Contrary to other class-level settings, `@Field`s\nwill not be inherited by child classes.\n\nForm Views\n----------\n\nIn the example we have defined additional form views in ``@Form\\Form``\nannotation so we can add another argument to ``createFormBuilder``\n\n``` php\n$form = $generator-\u003ecreateFormBuilder($person, 'personal')-\u003egetForm();\n```\n\nAnd we will get Form with properties specified in annotation. We can \nalso add/override fields and their properties like this:\n\n``` php\n/**\n * @Form\\Form(\n *  work = { \"salary\" = { \"attr\" = { \"class\" = \"foo\" } } }\n * )\n */\nclass Person\n```\n\nBut if you need something more sophisticated than Annotations we \nhave prepared few possibilities that can be either added manually\nor by tagging your services. For each of them FormGenerator allows \nyou to pass any additional informations you want in optional \n``$context`` argument. Both ways allows you to specify `priority`\nwhich defines order of execution (default is `0`, if two or more\nservices have same priority then first added is executed first).\n\n**If you have enabled [Service autoconfiguration](http://symfony.com/blog/new-in-symfony-3-3-service-autoconfiguration)\nthe bundle will automatically tag services for you.**\n\nFormViewProvider\n----------------\n\nThese are used to provide fields list and/or basic configuration\nfor Forms and are doing exactly same thing as ``@Form\\Form``\nannotation.\n\nTag for service: ``form_generator.view_provider``\n\nFormConfigurationModifier\n-------------------------\n\nThese can modify any form configuration provided by class\nitself or FormViewProviders. Feel free to remove or add more\nstuff to your Form or tweak existing configuration\n\nTag for service: ``form_generator.configuration_modifier``\n\n``` php\nclass InactivePersonModifier implements FormConfigurationModifierInterface\n{\n    public function modify($model, $configuration, $context) \n    {\n        unset($configuration['salary']);\n        return $configuration;\n    }\n\n    public function supports($model, $configuration, $context) \n    {\n        return $model instanceof Person \u0026\u0026 $model-\u003eactive === false;\n    }\n}\n```\n\nFormFieldResolver\n-----------------\n\nThese are responsible for creating actual field in Form and can\nbe used for instance to attach Transformers to your fields.\n\nTag for service: ``form_generator.field_resolver``\n\n``` php\nclass PersonSalaryResolver implements FormFieldResolverInterface\n{\n    public function getFormField(FormBuilderInterface $fb, $field, $type, $options, $context) \n    {\n        $transformer = new /* ... */;\n        return $fb-\u003ecreate($field, $type, $options)\n                -\u003eaddViewTransformer($transformer);\n    }\n\n    public function supports($model, $field, $type, $options, $context) \n    {\n        return $model instanceof Person \u0026\u0026 $field === 'salary';\n    }\n}\n```\n\nEmbedded Forms\n--------------\n\nIf you need embedded forms we got you covered:\n\n``` php\n/**\n * @Form\\Embed(class=\"Codete\\FormGeneratorBundle\\Tests\\Model\\Person\")\n */\npublic $person;\n```\n\nSuch sub-form will contain all annotated properties from given model.\nTo specify a view for the generated embedded form just specify it in\nthe configuration:\n\n``` php\n/**\n * @Form\\Embed(\n *  class=\"Codete\\FormGeneratorBundle\\Tests\\Model\\Person\",\n *  view=\"work\"\n * )\n */\npublic $employee;\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodete%2Fformgeneratorbundle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodete%2Fformgeneratorbundle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodete%2Fformgeneratorbundle/lists"}