{"id":24594619,"url":"https://github.com/fabrice8/markojs-form","last_synced_at":"2026-05-17T20:03:01.971Z","repository":{"id":57291376,"uuid":"386486430","full_name":"fabrice8/markojs-form","owner":"fabrice8","description":"Form Component Handler Library for MarkoJS","archived":false,"fork":false,"pushed_at":"2022-12-28T17:17:08.000Z","size":53,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-27T06:27:15.337Z","etag":null,"topics":["client-side-rendering","form","form-validator","javascript","markojs","nodejs","server-side-rendering"],"latest_commit_sha":null,"homepage":"https://fabrice8.github.io/markojs-form","language":"JavaScript","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/fabrice8.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":"2021-07-16T02:38:39.000Z","updated_at":"2023-08-11T20:43:40.000Z","dependencies_parsed_at":"2023-01-31T07:01:17.088Z","dependency_job_id":null,"html_url":"https://github.com/fabrice8/markojs-form","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/fabrice8/markojs-form","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabrice8%2Fmarkojs-form","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabrice8%2Fmarkojs-form/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabrice8%2Fmarkojs-form/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabrice8%2Fmarkojs-form/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fabrice8","download_url":"https://codeload.github.com/fabrice8/markojs-form/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabrice8%2Fmarkojs-form/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33153662,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-17T09:28:26.183Z","status":"ssl_error","status_checked_at":"2026-05-17T09:27:52.702Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["client-side-rendering","form","form-validator","javascript","markojs","nodejs","server-side-rendering"],"created_at":"2025-01-24T11:15:44.879Z","updated_at":"2026-05-17T20:03:01.953Z","avatar_url":"https://github.com/fabrice8.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"@MarkoJS Form Handler\n==================================================\n\nForm Component Handler Library for [MarkoJS]. This library simplify how to handle the flow within a [MarkoJS] form component by handling all input fields **events**, **auto-fill**, **auto-saving**, **auto-completition**, **validation** and the **form submission** by binding to the `this` of the component. Enough talking, let get to the facts.\n\n[![npm version][npm-badge]][npm]\n[![dependency status][dep-badge]][dep-status]\n[![Rate on Openbase](https://badges.openbase.com/js/rating/markojs-form.svg)](https://openbase.com/js/markojs-form?utm_source=embedded\u0026utm_medium=badge\u0026utm_campaign=rate-badge)\n\n[MarkoJS]: https://www.npmjs.org/package/markojs-form\n[npm]: https://www.npmjs.org/package/markojs-form\n[npm-badge]: https://img.shields.io/npm/v/markojs-form.svg?style=flat-square\n[dep-status]: https://david-dm.org/fabrice8/markojs-form\n[dep-badge]: https://img.shields.io/david/fabrice8/markojs-form.svg?style=flat-square\n\n\n## Installation\n\nInstall using npm\n\n```shell\n$ npm install markojs-form\n```\nInstall using yarn\n\n```shell\n$ yarn add markojs-form\n```\n\n## Usage\n\nCreate new instance of the FormHandler Object Class.\n\n```javascript\nconst FormHandler = require('markojs-form');\n\n// or \n\nimport FormHandler from 'markojs-form';\n\nstatic const options = {\n    key: 'form-id',\n    autosave: true,\n    crosscheck: true\n}\n\nstatic const fhandler = new FormHandler( options );\n\n// or inside a class method\n\nclass {\n    onCreate(){\n        // ...\n        this.fhandler = new FormHandler( options )\n    }\n}\n```\n\n**Important**: Avoid using form-handler instance globally.\n\n## Options\n\nAll options are optional\n\n* **key**: `String` Same as the component tag key of the form being handled: Eg. `\u003cform key=\"form-id\"\u003e...\u003c/form\u003e`\n* **autosave**: `Boolean` Define whether to auto-save input values when the user is filling the form. Very useful especially in SPA case when the user manually or accidentally refresh the page, the inputs will stay filled with the saved values. Default `false`\n* **crosscheck**: `Boolean` When submit form event is detected by **FormHandler**, it run a final validation cross-check through all inputs that require validation in the form before submitting it. Default: `false`\n\n\n## Methods\n\n### `bind(this, [formObject])`\n\nBind the form handler to the component. `formObject` is a object passed to the handler as the initial form fields \u0026 values: It's optional\n\n```javascript\n\nclass {\n    onCreate(){\n        this.state = {\n            // ...\n        }\n\n        fhandler.bind(this)\n    }\n}\n```\n\nWhen this is done, the handler add `form` \u0026 `formError` fields to the component state: Eg. `this.state.form`. Those contain respectively the form data and the error status of the form validation.\n\nAlso, couple of methods are automatically bind to the component's class, to listen and handle events that occured when interacting with the form.\n\n- ``__onInput`` Method handler for input event\n- ``__onChange`` Method handler for input change event\n- ``__onSelect`` Method handler for select input change event\n- ``__onChecked`` Method handler for checkbox and radio input events\n\nAll these methods assigned approprietly to the various form tags, empowers the **FormHandler** to collect and update the data and errors state of the form component.\n\n**Example**\n\nWhen filling the input form below, **FormHandler** automatically update `this.state.form.name` at every input \u0026 change events, allowing the component state to be updated.\n\n```marko\n\u003cinput type=\"text\"\n        name=\"name\"\n        value=state.form.name\n        on-input(\"__onInput\")\n        on-change(\"__onChange\")\u003e\n```\n\n**Note**: You can still assign your own method listener to handle inputs yourself. All is up to you.\n\n\n### `unbind(this)`\n\nUnbind the form handler to the component.\n\n\n### `set(name, value)`\n\nManually set a value to a form input.\n\n```javascript\nclass {\n    // ...\n    onMount(){\n        fhandler.set('fullname', 'John Dupont')\n    }\n}\n```\n\n### `fill(values)`\n\nBeside `Set()` method which can only set one field at a time, `fill()` method get an object of multiple fields \u0026 values to add manually to the form.\n\n```javascript\nclass {\n    // ...\n    onInput(){\n        fhandler.fill({ status: 'healty', age: 79, gender: 'female' })\n    }\n}\n```\n\n### `error(name, [status])`\n\nManually define the status of error of a given input. `status` can be a simple boolean: `true` meaning there's an error and `false` to cancel error. `status` can also be a string text message describing the error: Very usefull to display dynamic error messages to the user.\n\n```marko\nclass {\n    // ...\n    onSubmit(){\n        fhandler.error('age', 'This field is required')\n    }\n    onDestry(){\n        // Cancel the error\n        fhandler.error('age', false)\n    }\n}\n\n\u003cdiv\u003e\n    \u003cinput type=\"number\"\n            name=\"age\"\n            value=state.form.age\n            on-change(\"__onChange\")\u003e\n    \u003c!-- Dislay error message --\u003e\n    \u003cif( state.formError.age )\u003e\n        \u003cp\u003e${state.formError.age || 'Your custom error'}\u003c/p\u003e\n    \u003c/if\u003e\n\u003c/div\u003e\n```\n\n### `reset()`\n\nSet the form back to its initial state. This method is automatilly call when the form get submited. It's also **recommanded** to call it when the form component `onDestroy` event occurs: Proper clean up\n\n```javascript\nclass {\n    onCreate(){\n        const initialForm = { username: 'gremdol', email: 'my@email.com' }\n\n        fhandler.bind(this, initialForm)\n    }\n    onDestroy(){\n        // Reset the form.\n        fhandler.reset()\n    }\n}\n```\n\n### `alert(message, status)`\n\nDisplay general alert of the form component\n\n```marko\nclass {\n    onSubmit(){\n        fhandler.alert('Unexpected Error Occured! Try again', 'warning')\n    }\n}\n\n\u003cFormAlert alert=state.alert/\u003e\n```\n\n## Form Validation\n\n**FormHandler** has couple of in-build validation rules that you can use.\n- `required`: Specify that the input field is required\n- `url`: Check whether the input value is a valid URL\n- `domain`: Check whether the input value is a valid Domain/Hostname\n- `email`: Check whether the input value is a valid email address\n- `phone`: Check whether the input value is a valid phone number\n- `fullname`: Check whether the input value is a valid user fullname\n- `password`: check and return password by strengh\n- `number`: Input value must be a valid number\n- `boolean`: Input value must be True or False\n- `array`: Check whether the input value is an Array: Usually set manually\n- `object`: Check whether the input value is an Object: Usually set manually\n- `lowercase`: Input value must be only a lowercase string\n- `uppercase`: Input value must be onlye an uppercase string\n- `length`: Specify the required length of the input value\n- `minLength`: Specify the minimun length of the input value\n- `maxLength`: Specify the maximum length of the input value\n\n\n### `define(rule)`\n\nDefine your own custom validation rules or overwrite in-build rules. There are 2 ways to define a validation rules: \nstatically using `static or $` or inside the component's class methods or right into the template:\n\n**Examples**\n\n```marko\nclass {\n    onCreate(){\n        fhandler.define([{ name: 'custom1', regexp: '/\\.+/' }, { name: 'custom2', regexp: '/\\w+/' }])\n    }\n}\n\n\u003cdiv\u003e\n    \u003c!-- Use inbuild rules --\u003e\n    \u003cinput type=\"email\"\n            name=\"email\"\n            value=state.form.email\n            validate=\"email\"\n            on-change(\"__onChange\")\u003e\n            \n    \u003c!-- Use an already defined custom rules --\u003e\n    \u003cinput type=\"text\"\n            name=\"name\"\n            value=state.form.name\n            validate=\"custom1\"\n            on-change(\"__onChange\")\u003e\n            \n    \u003c!-- Define and assign custom rules right way --\u003e\n    \u003cinput type=\"text\"\n            name=\"orderId\"\n            value=state.form.orderId\n            validate=fhandler.define([{ name: 'orderId', regexp: '/PT-(\\b+{4,})-00/' }])\n            on-change(\"__onChange\")\u003e\n\u003c/div\u003e\n```\n\n### `validate(rule, value)`\n\nManually validate a form field.\n\n```javascript\nclass {\n    // ... \n\n    onCustomHandler( value ){\n        const isValid = fhandler.validate( 'email', value )\n\n        // ...\n    }\n}\n```\n\n\n## Events\n\n**FormHandler** instanciate [EventEmitter][] Object. Here a couple of events triggered by the form handler: \n\n* `bind`: When **FormHandler** get bind to the component\n* `unbind`: When **FormHandler** get unbind to the component\n* `input`: New form input\n* `error`: When an error occured\n* `fill`: When `fill()` method get called\n* `autosave`: When new auto-save record occured\n* `reset`: When form get reset\n\n### `on(event, fn)`\n\nRegister an event listener\n\n### `off(event, fn)`\n\nRemove an event listener\n\n### `removeListeners()`\n\nRemove all event listeners\n\n\nVoilà!\n\n\nFeedback \u0026 Contribution\n-------\n\nYou know the say: No one is whole alone! So, feedbacks and the smallest contributions you can think of are all welcome. Kindly report any encounted [Issues here][] and I'll be glad to work on it right away. Thank you.\n\n\nLicense\n-------\n\nThis software is free to use under the MIT license. See the [LICENSE file][] for license text and copyright information.\n\n\n[LICENSE file]: https://github.com/fabrice8/markojs-form/blob/master/LICENSE\n[Issues here]: https://github.com/fabrice8/markojs-form/issues\n[EventEmitter]: https://www.npmjs.com/package/EventEmitter","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffabrice8%2Fmarkojs-form","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffabrice8%2Fmarkojs-form","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffabrice8%2Fmarkojs-form/lists"}