{"id":20173830,"url":"https://github.com/nebkam/symfony-traits","last_synced_at":"2025-04-10T03:17:58.830Z","repository":{"id":57024548,"uuid":"126334752","full_name":"nebkam/symfony-traits","owner":"nebkam","description":"Few helper traits for quicker API development in Symfony","archived":false,"fork":false,"pushed_at":"2024-08-09T05:47:44.000Z","size":35,"stargazers_count":16,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-10T03:17:51.271Z","etag":null,"topics":["form","helpers","symfony"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nebkam.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-03-22T12:52:34.000Z","updated_at":"2024-08-09T05:45:41.000Z","dependencies_parsed_at":"2024-08-09T06:44:00.386Z","dependency_job_id":null,"html_url":"https://github.com/nebkam/symfony-traits","commit_stats":{"total_commits":29,"total_committers":4,"mean_commits":7.25,"dds":0.3793103448275862,"last_synced_commit":"09c04a69ee7f7b30149475c4bc2e16a3d64c07cc"},"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nebkam%2Fsymfony-traits","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nebkam%2Fsymfony-traits/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nebkam%2Fsymfony-traits/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nebkam%2Fsymfony-traits/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nebkam","download_url":"https://codeload.github.com/nebkam/symfony-traits/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248148247,"owners_count":21055548,"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":["form","helpers","symfony"],"created_at":"2024-11-14T01:38:05.209Z","updated_at":"2025-04-10T03:17:58.798Z","avatar_url":"https://github.com/nebkam.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Latest Stable Version](https://poser.pugx.org/nebkam/symfony-traits/v)](//packagist.org/packages/nebkam/symfony-traits)\n\n# Symfony Traits\nFew helper traits for quicker API  development in Symfony\n\n## FormTrait\n### `handleJSONForm` \n- controller helper method for JSON data sent in `POST`, `PUT` or `PATCH` request content\n- generally speaking, sending JSON content is more flexible than forms, for CRUD with JS frameworks.\n\n```php\npublic function create(Request $request)\n\t{\n\t$entity = new Entity();\n\t$this-\u003ehandleJSONForm($request, $entity, EntityType::class, $options = [], $clearMissingFields = true);\n\t// persist and flush $entity\n```\n\n```php\npublic function edit(Request $request,Entity $entity)\n\t{\n\t$this-\u003ehandleJSONForm($request, $entity, EntityType::class, $options = [], $clearMissingFields = true);\n\t// flush entity\n```\n### `handleForm` \n- controller helper method for traditional form data in `GET` or `POST`\n- I advice using traditional form data only when JSON is out of place (i.e. `GET` params)\n\n```php\npublic function example(Request $request)\n\t{\n\t$domain = new Domain();\n\tif ($request-\u003equery-\u003ecount() \u003e 0)\n\t\t{\n\t\t$this-\u003ehandleForm($request, $params, DomainType::class, $options = [], $clearMissingFields = true);\n\t\t}\n\t\n\t// do something with $domain\n```\n\n### `handleUpload`\n- controller helper method to validate a single file upload\n- a lightweight alternative to a [File constraint](http://symfony.com/doc/current/reference/constraints/File.html), when you need just one file, not the whole form\n\n```php\npublic function uploadImage(Request $request)\n\t{\n\t$file = $this-\u003ehandleUpload($request, 'image');\n\t\n\t// do something with $file\n\t}\n```\n\n### `ValidationExceptionListener`\nSince all `handle*` methods in this trait throw a `Nebkam\\SymfonyTraits\\ValidationException`, you have to catch it, either via `try {..} catch` in the controller or via global exception listener.\nTo ease this, this package includes a sample exception listener, which returns validation errors in JSON. You just have to register it as a service:\n\n```yaml\nNebkam\\SymfonyTraits\\EventListener\\ValidationExceptionListener:\n    tags:\n        - { name: kernel.event_listener, event: kernel.exception }\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnebkam%2Fsymfony-traits","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnebkam%2Fsymfony-traits","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnebkam%2Fsymfony-traits/lists"}