-
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f74c388
commit 3f2a384
Showing
11 changed files
with
301 additions
and
33 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# FORM | ||
|
||
## Form Constructor | ||
|
||
new Form({ fields, schema, options, extend }); | ||
|
||
* **fields**: represents the fields: name, label, value. | ||
|
||
* **schema**: the json-schema for the validation. | ||
See [json-schema.org](http://json-schema.org) | ||
|
||
* **options** the additional options for ajv. | ||
See [github.com/epoberezkin/ajv#options](https://github.com/epoberezkin/ajv#options) | ||
|
||
* **extend** add custom validation keyword for using in the json-schema | ||
|
||
## Form API | ||
|
||
#### syncValue() | ||
Synchronizes the value of the field `onChange` event. | ||
|
||
You must define the name of the field to use this method. | ||
|
||
#### isValid() | ||
Check if the form is valid (boolean). | ||
|
||
#### isDirty() | ||
Check if the form is dirty (boolean). | ||
|
||
#### fieldKeys() | ||
Get an array with all fields names. | ||
|
||
#### values() | ||
Get an object with all fields values. | ||
|
||
#### clear() | ||
Clear the form to empty values. | ||
|
||
#### reset() | ||
Reset the form to initials values. | ||
|
||
#### update(obj) | ||
Pass an object to update the form with new values. | ||
|
||
#### validate() | ||
Check if the form is valid (boolean). | ||
|
||
#### invalidate(err) | ||
Invalidate the form passing a generic error message (string). | ||
|
||
--- | ||
|
||
# Custom Validation Keyword | ||
|
||
``` | ||
// define an 'extend' object with the custom keyword | ||
const extend = { | ||
range: { | ||
type: 'number', | ||
compile: (sch, parentSchema) => { | ||
const min = sch[0]; | ||
const max = sch[1]; | ||
return parentSchema.exclusiveRange === true | ||
? (data) => (data > min && data < max) | ||
: (data) => (data >= min && data <= max); | ||
}, | ||
}, | ||
}; | ||
// then use it the schema | ||
var schema = { "range": [2, 4], "exclusiveRange": true }; | ||
// pass all to the form constructor | ||
new Form({ fields, schema, extend }); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import Form from '../../src/index'; | ||
import extend from './extend'; | ||
|
||
const fields = { | ||
username: { | ||
label: 'Username', | ||
value: 'SteveJobs', | ||
}, | ||
email: { | ||
label: 'Email', | ||
value: '[email protected]', | ||
}, | ||
password: { | ||
label: 'Password', | ||
value: 'thinkdifferent', | ||
}, | ||
devSkills: { | ||
label: 'Dev Skills', | ||
value: 5, | ||
}, | ||
}; | ||
|
||
const schema = { | ||
type: 'object', | ||
properties: { | ||
username: { type: 'string', minLength: 6, maxLength: 20 }, | ||
email: { type: 'string', format: 'email', minLength: 5, maxLength: 20 }, | ||
password: { type: 'string', minLength: 6, maxLength: 20 }, | ||
devSkills: { range: [5, 10] }, | ||
}, | ||
}; | ||
|
||
export default new Form({ fields, schema, extend }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import Form from '../../src/index'; | ||
import extend from './extend'; | ||
|
||
const fields = { | ||
username: { | ||
label: 'Username', | ||
}, | ||
email: { | ||
label: 'Email', | ||
}, | ||
password: { | ||
label: 'Password', | ||
}, | ||
devSkills: { | ||
label: 'Dev Skills', | ||
}, | ||
}; | ||
|
||
const schema = { | ||
type: 'object', | ||
properties: { | ||
username: { type: 'string', minLength: 6, maxLength: 20 }, | ||
email: { type: 'string', format: 'email', minLength: 5, maxLength: 20 }, | ||
password: { type: 'string', minLength: 6, maxLength: 20 }, | ||
devSkills: { range: [1, 10] }, | ||
}, | ||
}; | ||
|
||
export default new Form({ fields, schema, extend }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import Form from '../../src/index'; | ||
import extend from './extend'; | ||
|
||
const fields = { | ||
username: { | ||
label: 'Username', | ||
value: '', | ||
}, | ||
email: { | ||
label: 'Email', | ||
value: '', | ||
}, | ||
password: { | ||
label: 'Password', | ||
value: '', | ||
}, | ||
devSkills: { | ||
label: 'Dev Skills', | ||
value: 1, | ||
}, | ||
}; | ||
|
||
const schema = { | ||
type: 'object', | ||
properties: { | ||
username: { type: 'string', minLength: 6, maxLength: 20 }, | ||
email: { type: 'string', format: 'email', minLength: 5, maxLength: 20 }, | ||
password: { type: 'string', minLength: 6, maxLength: 20 }, | ||
devSkills: { range: [1, 10] }, | ||
}, | ||
}; | ||
|
||
export default new Form({ fields, schema, extend }); |
Oops, something went wrong.