React Native Form Validation
Overview
Simple JS component for validating forms. Works with deep nested objects.
Installation
npm i react-native-form-validation --save
Components
1. Form
Simple View component.
Methods:
-
validate
Validates form. Returns array of objects. Each object is for each
FormItem
inForm
. Each object has aisValid
property. If form was validated successfully -submit
callback will be fired.let validatedFields = this.refs.form.validate(); validatedFields = [ { isValid: true, ...other props }, { isValid: false, ...other props } ]
Props:
Name | Type | Description |
---|---|---|
shouldValidate | boolean | Describes should form be validated before calling submit function. If form is not valid - submit function not called. false by default |
submit | function | Simple callback. Will be fired if form is valid or validation is not required |
style | object | Simple style object. |
2. FormItem
Simple View component. Each Form
children should be wrapped in FormItem
.
Props:
Name | Type | Description |
---|---|---|
isRequired | boolean | Describes should this FormItem be validated. false by default |
regExp | object | Regular expression for validation. null by default |
fieldToBeValidated | string | Name of the property by which we should validate FormItem . 'value' by default |
validationFunction | function | Custom validation function. Should return boolean . NOTICE! If validationFunction is defined - default validation for empty value and regular expression with regExp property will not be called |
style | object | Simple style object. |
Example
;;; const width = Dimensionswidth; { superprops; } { return <View style=thispropsstyle> <TextInput style=stylesflex value=thispropsvalue onChange=thispropsonChange/> </View> ; } { superprops; thisstate = textInput1:'1' textInput2:'2' textInput3:'3' textInput4:'4' view1:'1' ; } { this; } { this; } { this; } { this; } { let submitResults = thisrefsform; } { return true; } { return <View style=stylescontainer> <Form ref="form" shouldValidate=true style=stylesflex> <FormItem isRequired=true regExp=/^\d+$/ style=stylesformInput> <TextInput style=stylesfirstInput value=thisstatetextInput1 onChange=thistextInput1Change/> </FormItem> <FormItem isRequired=false style=stylesformInput> <View style=stylesflex> <View style=stylessecondInputWrapper> <TextInput style=stylesflex value=thisstatetextInput2 onChange=thistextInput2Change/> </View> </View> </FormItem> <FormItem isRequired=true validationFunction=thiscustomValidation> <View style=stylesformItem> <View style=stylesflex> <View style=stylesflex> <TextInput style=stylesflex value=thisstatetextInput3 onChange=thistextInput3Change/> </View> <View /> </View> </View> </FormItem> <FormItem isRequired=true> <ComponentWithValue style=stylesformItem value=thisstatetextInput4 onChange=thistextInput4Change/> </FormItem> <FormItem isRequired=true fieldToBeValidated='test'> <View style=stylesformItem test=thisstateview1> <Text> thisstateview1</Text> </View> </FormItem> </Form> <TouchableOpacity style=stylessubmitBtn onPress=thissubmit> <Text>Submit</Text> </TouchableOpacity> </View> ; } { return <FormTest /> ; } const styles = container: alignItems:'center' justifyContent:'center' width flex:1 flex: flex:1 formItem: width:300 height:50 formInput: width:300 height:50 firstInput: width:300 height:60 secondInputWrapper: width:300 height:50 submitBtn: height:100 width:100 ;
License
MIT