{"id":16347511,"url":"https://github.com/oscarotero/form-manager","last_synced_at":"2025-04-05T02:09:46.025Z","repository":{"id":5581157,"uuid":"6787207","full_name":"oscarotero/form-manager","owner":"oscarotero","description":"PHP library to create and validate html forms","archived":false,"fork":false,"pushed_at":"2022-08-22T07:33:56.000Z","size":769,"stargazers_count":152,"open_issues_count":4,"forks_count":42,"subscribers_count":16,"default_branch":"master","last_synced_at":"2024-10-12T00:42:57.350Z","etag":null,"topics":["form-builder","forms"],"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/oscarotero.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2012-11-20T23:40:42.000Z","updated_at":"2024-10-07T09:47:40.000Z","dependencies_parsed_at":"2022-08-26T18:21:41.088Z","dependency_job_id":null,"html_url":"https://github.com/oscarotero/form-manager","commit_stats":null,"previous_names":[],"tags_count":65,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oscarotero%2Fform-manager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oscarotero%2Fform-manager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oscarotero%2Fform-manager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oscarotero%2Fform-manager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oscarotero","download_url":"https://codeload.github.com/oscarotero/form-manager/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247276189,"owners_count":20912288,"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-builder","forms"],"created_at":"2024-10-11T00:43:10.111Z","updated_at":"2025-04-05T02:09:46.018Z","avatar_url":"https://github.com/oscarotero.png","language":"PHP","funding_links":[],"categories":["Tools"],"sub_categories":[],"readme":"# FormManager\r\n\r\n[![testing](https://github.com/oscarotero/form-manager/actions/workflows/main.yaml/badge.svg)](https://github.com/oscarotero/form-manager/actions/workflows/main.yaml)\r\n\r\n\u003e ### Note: this is the documentation of FormManager 6.x \r\n\u003e For v5.x version [Click here](https://github.com/oscarotero/form-manager/tree/v5)\r\n\r\n## Installation:\r\n\r\nThis package requires `PHP\u003e=7.1` and is available on [Packagist](https://packagist.org/packages/form-manager/form-manager):\r\n\r\n```\r\ncomposer require form-manager/form-manager\r\n```\r\n\r\n## Create a field\r\n\r\nFormManager is namespaced, but you only need to import a single class into your context:\r\n\r\n```php\r\nuse FormManager\\Factory as F;\r\n```\r\n\r\nUse the imported factory to create all form elements:\r\n\r\n```php\r\n//Create an input type=\"text\" element\r\n$name = F::text();\r\n\r\n//Create the input with a label\r\n$name = F::text('Please, introduce your name');\r\n\r\n//Or with extra attributes\r\n$name = F::text('Please, introduce your name', ['class' =\u003e 'name-field']);\r\n\r\n//Add or remove attributes\r\n$name-\u003esetAttribute('title', 'This is the name input');\r\n$name-\u003eremoveAttribute('class');\r\n$name-\u003esetAttributes([\r\n    'required',\r\n    'readonly',\r\n    'tabindex' =\u003e 2,\r\n    'maxlength' =\u003e 50\r\n]);\r\n\r\n//Set the value\r\n$name-\u003esetValue('MyName');\r\n\r\n//Use magic properties to get/set/remove attributes\r\n$name-\u003eclass = 'name-field';\r\n$name-\u003erequired = false;\r\nunset($name-\u003ereadonly);\r\n```\r\n\r\n### List of all available inputs:\r\n\r\nAll HTML5 field types are supported:\r\n\r\n* `F::checkbox($label, $attributes)`\r\n* `F::color($label, $attributes)`\r\n* `F::date($label, $attributes)`\r\n* `F::datetimeLocal($label, $attributes)`\r\n* `F::email($label, $attributes)`\r\n* `F::file($label, $attributes)`\r\n* `F::hidden($value, $attributes)`\r\n* `F::month($label, $attributes)`\r\n* `F::number($label, $attributes)`\r\n* `F::password($label, $attributes)`\r\n* `F::radio($label, $attributes)`\r\n* `F::range($label, $attributes)`\r\n* `F::search($label, $attributes)`\r\n* `F::select($label, $options, $attributes)`\r\n* `F::submit($label, $attributes)`\r\n* `F::tel($label, $attributes)`\r\n* `F::text($label, $attributes)`\r\n* `F::textarea($label, $attributes)`\r\n* `F::time($label, $attributes)`\r\n* `F::url($label, $attributes)`\r\n* `F::week($label, $attributes)`\r\n\r\n\u003e Note that all inputs accepts the same arguments except `hidden` and `select`.\r\n\r\n## Validation\r\n\r\nThis library uses internally [symfony/validation](https://symfony.com/doc/current/validation.html) to perform basic html5 validations and error reporting. HTML5 validation attributes like `required`, `maxlength`, `minlength`, `pattern`, etc are supported, in addition to intrinsic validations assigned to each input like email, url, date, etc.\r\n\r\n```php\r\n$email = F::email();\r\n\r\n$email-\u003esetValue('invalid-email');\r\n\r\n//Validate the value\r\nif ($email-\u003eisValid()) {\r\n    return true;\r\n}\r\n\r\n//Get errors\r\n$error = $email-\u003egetError();\r\n\r\n//Print the first error message\r\necho $error;\r\n\r\n//Iterate through all messages\r\nforeach ($error as $err) {\r\n    echo $err-\u003egetMessage();\r\n}\r\n\r\n//You can also customize/translate the error messages\r\n$email-\u003esetErrorMessages([\r\n    'email' =\u003e 'The email is not valid',\r\n    'required' =\u003e 'The email is required',\r\n    'maxlength' =\u003e 'The email is too long, it must have {{ limit }} characters or less',\r\n]);\r\n\r\n//And add more symfony validators\r\n$ip = F::text();\r\n$ip-\u003eaddConstraint(new Constraints\\Ip());\r\n```\r\n\r\nSee [all constraints supported by symfony](https://symfony.com/doc/current/validation.html#supported-constraints)\r\n\r\n\r\n## Render html\r\n\r\n```php\r\n$name = F::text('What is your name?', ['name' =\u003e 'name']);\r\n\r\necho $name;\r\n```\r\n```html\r\n\u003clabel for=\"id-input-1\"\u003eWhat is your name?\u003c/label\u003e \u003cinput id=\"id-input-1\" type=\"text\" name=\"name\"\u003e\r\n```\r\n\r\nSet a custom template using `{{ label }}` and `{{ input }}` placeholders:\r\n\r\n```php\r\n$name-\u003esetTemplate('{{ label }} \u003cdiv class=\"input-content\"\u003e{{ input }}\u003c/div\u003e');\r\necho $name;\r\n```\r\n```html\r\n\u003clabel for=\"id-input-1\"\u003eWhat is your name?\u003c/label\u003e \u003cdiv class=\"input-content\"\u003e\u003cinput id=\"id-input-1\" type=\"text\" name=\"name\"\u003e\u003c/div\u003e\r\n```\r\n\r\nIf you want to wrap the previous template in a custom html, use the `{{ template }}` placeholder:\r\n\r\n```php\r\n$name-\u003esetTemplate('\u003cfieldset\u003e{{ template }}\u003c/fieldset\u003e');\r\necho $name;\r\n```\r\n```html\r\n\u003cfieldset\u003e\u003clabel for=\"id-input-1\"\u003eWhat is your name?\u003c/label\u003e \u003cdiv class=\"input-content\"\u003e\u003cinput id=\"id-input-1\" type=\"text\" name=\"name\"\u003e\u003c/div\u003e\u003c/fieldset\u003e\r\n```\r\n\r\n## Grouping fields\r\n\r\nGroup the fields to follow a specific data structure:\r\n\r\n### Group\r\n\r\nGroups allow to place a set of inputs under an specific name:\r\n\r\n```php\r\n$group = F::group([\r\n    'name' =\u003e F::text('Username'),\r\n    'email' =\u003e F::email('Email'),\r\n    'password' =\u003e F::password('Password'),\r\n]);\r\n\r\n$group-\u003esetValue([\r\n    'name' =\u003e 'oscar',\r\n    'email' =\u003e 'oom@oscarotero.com',\r\n    'password' =\u003e 'supersecret',\r\n]);\r\n```\r\n\r\n### Radio group\r\n\r\nSpecial case for radios where all inputs share the same name with different values:\r\n\r\n```php\r\n$radios = F::radioGroup([\r\n    'red' =\u003e 'Red',\r\n    'blue' =\u003e 'Blue',\r\n    'green' =\u003e 'Green',\r\n]);\r\n\r\n$radios-\u003esetValue('blue');\r\n```\r\n\r\n### Submit group\r\n\r\nSpecial case to group several submit buttons under the same name but different values:\r\n\r\n```php\r\n$buttons = F::submitGroup([\r\n    'save' =\u003e 'Save the row',\r\n    'duplicate' =\u003e 'Save as new row',\r\n]);\r\n\r\n$buttons-\u003esetName('action');\r\n```\r\n\r\n### Group collection\r\n\r\nIs a collection of values using the same group:\r\n\r\n```php\r\n$groupCollection = F::groupCollection(\r\n    f::group([\r\n        'name' =\u003e F::text('Name'),\r\n        'genre' =\u003e F::radioGroup([\r\n            'm' =\u003e 'Male',\r\n            'f' =\u003e 'Female',\r\n            'o' =\u003e 'Other',\r\n        ]),\r\n    ])\r\n]);\r\n\r\n$groupCollection-\u003esetValue([\r\n    [\r\n        'name' =\u003e 'Oscar',\r\n        'genre' =\u003e 'm'\r\n    ],[\r\n        'name' =\u003e 'Laura',\r\n        'genre' =\u003e 'f'\r\n    ],\r\n])\r\n```\r\n\r\n### Multiple group collection\r\n\r\nIs a collection of values using various groups, using the field `type` to identify which group is used by each row:\r\n\r\n```php\r\n$multipleGroupCollection = F::multipleGroupCollection(\r\n    'text' =\u003e f::group([\r\n        'type' =\u003e F::hidden(),\r\n        'title' =\u003e F::text('Title'),\r\n        'text' =\u003e F::textarea('Body'),\r\n    ]),\r\n    'image' =\u003e f::group([\r\n        'type' =\u003e F::hidden(),\r\n        'file' =\u003e F::file('Image file'),\r\n        'alt' =\u003e F::text('Alt text'),\r\n        'text' =\u003e F::textarea('Caption'),\r\n    ]),\r\n    'link' =\u003e f::group([\r\n        'type' =\u003e F::hidden(),\r\n        'text' =\u003e F::text('Link text'),\r\n        'href' =\u003e F::url('Url'),\r\n        'target' =\u003e F::select([\r\n            '_blank' =\u003e 'New window',\r\n            '_self' =\u003e 'The same window',\r\n        ]),\r\n    ]),\r\n]);\r\n\r\n$multipleGroupCollection-\u003esetValue([\r\n    [\r\n        'type' =\u003e 'text',\r\n        'title' =\u003e 'Welcome to my page',\r\n        'text' =\u003e 'I hope you like it',\r\n    ],[\r\n        'type' =\u003e 'image',\r\n        'file' =\u003e 'avatar.jpg',\r\n        'alt' =\u003e 'Image of mine',\r\n        'text' =\u003e 'This is my photo',\r\n    ],[\r\n        'type' =\u003e 'link',\r\n        'text' =\u003e 'Go to my webpage',\r\n        'href' =\u003e 'https://oscarotero.com',\r\n        'target' =\u003e '_self',\r\n    ],\r\n]);\r\n```\r\n\r\n## Datalist\r\n\r\n[Datalist](http://www.w3.org/TR/html5/forms.html#the-datalist-element) are also allowed, just use the `createDatalist()` method:\r\n\r\n```php\r\n$input = F::search();\r\n\r\n$datalist = $input-\u003ecreateDatalist([\r\n    'female' =\u003e 'Female',\r\n    'male' =\u003e 'Male'\r\n]);\r\n\r\necho $input;\r\necho $datalist;\r\n```\r\n\r\n## Forms\r\n\r\nWe need a form to put all this things together.\r\n\r\n```php\r\n$loginForm = F::form([\r\n    'username' =\u003e F::text('User name'),\r\n    'password' =\u003e F::password('Password'),\r\n    '' =\u003e F::submit('Login'),\r\n]);\r\n\r\n$loginForm-\u003esetAttributes([\r\n    'action' =\u003e 'login.php',\r\n    'method' =\u003e 'post',\r\n]);\r\n\r\n//Load data from globals $_GET, $_POST, $_FILES\r\n$loginForm-\u003eloadFromGlobals();\r\n\r\n//Load data passing the arrays\r\n$loginForm-\u003eloadFromArrays($_GET, $_POST, $_FILES);\r\n\r\n//Or load from PSR-7 server request\r\n$loginForm-\u003eloadFromServerRequest($serverRequest);\r\n\r\n//Get loaded data\r\n$data = $loginForm-\u003egetValue();\r\n\r\n//Print the form\r\necho $loginForm;\r\n\r\n//Access to specific inputs:\r\necho $loginForm-\u003egetOpeningTag();\r\necho '\u003ch2\u003eLogin:\u003c/h2\u003e';\r\n\r\necho $loginForm['username'];\r\necho '\u003chr\u003e';\r\necho $loginForm['password'];\r\necho '\u003chr\u003e';\r\necho $loginForm[''];\r\necho $loginForm-\u003egetClosingTag();\r\n\r\n//Iterate with all inputs\r\necho $loginForm-\u003egetOpeningTag();\r\necho '\u003ch2\u003eLogin:\u003c/h2\u003e';\r\n\r\nforeach ($loginForm as $input) {\r\n    echo \"\u003cdiv\u003e{$input}\u003c/div\u003e\";\r\n}\r\necho $loginForm-\u003egetClosingTag();\r\n```\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foscarotero%2Fform-manager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foscarotero%2Fform-manager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foscarotero%2Fform-manager/lists"}