react-superform
Form with superpowers for ReactJS
Tired of writing validation yourself? Tired of implementing custom elements for inputs when you already know HTML5 forms? React Superform to the rescue!
Installation
npm i -S react-superform
Getting Started
React Superform lets you create validatable <form>
with HTML5 form elements regardless of HTML5 validation support. Create your component and extend Superform
class instead of React.Component
use inherited methods to make your form awesome and user friendly!
; { console; } {} { return <form noValidate onSubmit= thishandleSubmit > <input type="email" // validate email ref="email" // ref is required to read the attributes name="email" // name field value= this onChange= thishandleChange required // field is required /> <p className="error"> this </p> <input type="submit" /> </form> ; } ReactDOM;
That's it, you are ready to go!
Examples
You can also check examples
dir.
Available HTML5 validation methods
max
maxLength
min
minLength
pattern
required
type="email"
type="number"
type="url"
Available custom validation methods
Equality
data-equals="<other field name>"
check whether field is the same as other field.
Warning: Avoid deep circular equality check. A -> B -> A
will end up with exception but A -> B -> C -> A
can freeze the tab.
Custom messages
data-messages="<custom messages>"
allows to define custom messages. It accepts JSON object with keys corresponding to failed rules.
Contributors
- Super logo author: Marcin Michalak
- Main Maintainer: Michal Zalecki
Superform
Superform's Superclass
Kind: global class
- Superform
- .onSuccessSubmit(data)
- .onErrorSubmit(errors, data)
- .handleChange(event) ⇒
Promise
- .handleSubmit(event) ⇒
Promise
- .markAsSubmitted() ⇒
Promise
- .isSubmited() ⇒
boolean
- .getValueOf(name) ⇒
string
|boolean
|undefined
- .linkStateOf(name) ⇒
Object
- .getData() ⇒
Object
- .getErrors() ⇒
Object
- .getErrorsOf(name) ⇒
Array
- .isFormValid() ⇒
boolean
- .isFieldValid(name) ⇒
boolean
- .getErrorMessageOf(name) ⇒
string
superform.onSuccessSubmit(data)
Called on form success submission.
Kind: instance method of Superform
Param | Type | Description |
---|---|---|
data | Object |
Form data with all fields valid |
superform.onErrorSubmit(errors, data)
Called on form failure submission.
Kind: instance method of Superform
Param | Type | Description |
---|---|---|
errors | Object |
Form errors object |
data | Object |
Form data |
Promise
superform.handleChange(event) ⇒ Handler for input change called internally.
Kind: instance method of Superform
Returns: Promise
- Promise resolved when data and errors are set if any
Param |
---|
event |
Promise
superform.handleSubmit(event) ⇒ Handler for form submission. Your form should call it as onSubmit
handler.
Kind: instance method of Superform
Returns: Promise
- Promise resolved with onSuccessSubmit
or onErrorSubmit
result
Param |
---|
event |
Promise
superform.markAsSubmitted() ⇒ Marks form as submitted by setting this.status.submitted
to true
.
Kind: instance method of Superform
Returns: Promise
- Promise resolved after state is set.
boolean
superform.isSubmited() ⇒ Determines whether form was submitted.
Kind: instance method of Superform
string
| boolean
| undefined
superform.getValueOf(name) ⇒ Returns value of specified field.
Kind: instance method of Superform
Returns: string
| boolean
| undefined
- Field value
Param | Type | Description |
---|---|---|
name | string |
Field name |
Object
superform.linkStateOf(name) ⇒ Links field value with form state. Simulates two way data binding.
Kind: instance method of Superform
Param | Type | Description |
---|---|---|
name | string |
Field name |
Object
superform.getData() ⇒ Returns form data.
Kind: instance method of Superform
Returns: Object
- Form data which is this.state.data
Object
superform.getErrors() ⇒ Returns form errors.
Kind: instance method of Superform
Returns: Object
- Form data which is this.state.errors
Array
superform.getErrorsOf(name) ⇒ Returns errors of specified field.
Kind: instance method of Superform
Param | Type | Description |
---|---|---|
name | string |
Field name |
boolean
superform.isFormValid() ⇒ Determines whether form is valid or not based on form errors.
Kind: instance method of Superform
boolean
superform.isFieldValid(name) ⇒ Determines whether field is valid or not based on form errors.
Kind: instance method of Superform
Param | Type | Description |
---|---|---|
name | string |
Field name |
string
superform.getErrorMessageOf(name) ⇒ Returns final error message for particular field
Kind: instance method of Superform
Returns: string
- Error message for the field
Param | Type | Description |
---|---|---|
name | string |
Field name |