react-native-validator
desc
- This is a form validation tool,like jQuery.validator.I mean that it is very easy to use.
- demo
- Provides two common verification methods (1. Click to verify all form elements and submit them; 2. Automatically verify during form input. If all form verification passes the button, you can click Submit)
- You can cover all styles
- It is support for dynamic forms including objects and arrays
- At present, the verification rules are written in elFormItem, and it is not considered to be added to elForm for the time being. Because it already meets the general requirements
- If you get an error after downloading npm, it is recommended to copy the src file under the package to your local and use it as your local file (I cannot explain why this is the case for now).
doc
Installation
npm i react-native-validator
Usage
{ superprops; thisstate = dynamicValidateForm: name:'' name2:"" phone:"" picker:"" nickname:"" ; } { let dynamicValidateForm =thisstate; return <View style=marginHorizontal:10> <FormelForm model=dynamicValidateForm ref="dynamicValidateForm"> <FormelFormItem label="name:" prop="name" rules= required: true message: 'name' > <TextInput style= height: 40 borderColor: 'gray' borderWidth: 1 value=dynamicValidateFormname placeholder="name" onChangeText= this /> </FormelFormItem> <FormelFormItem label="phonne:" prop="phone" rules= required: true message: 'Please enter numerals' pattern: /^\d{6}$/ message: 'Please enter 6 Arabic numerals' > <TextInput style= height: 40 borderColor: 'gray' borderWidth: 1 value=dynamicValidateFormphone placeholder="phone" onChangeText= this /> </FormelFormItem> <FormelFormItem label="nickname:" prop="nickname" checkOnBlur=true rules= required: true message: 'nicknname' > <FormelInput value=dynamicValidateFormnickname placeholder="check after blur" onChangeText= this /> </FormelFormItem> <FormelFormItem label="picker:" prop="picker" rules= required: true message: 'picker' > <Picker selectedValue=thisstatedynamicValidateFormpicker style=height: 200 width: 100borderColor: 'gray' borderWidth: 1 onValueChange= this > <PickerItem label="picker" value="" /> <PickerItem label="Java" value="java" /> <PickerItem label="JavaScript" value="js" /> <PickerItem label="css" value="css" /> </Picker> </FormelFormItem> <View> <TouchableOpacity onPress=this> <View style=stylesnormalBtn> <Text style=stylesnormalBtnTxt>submit</Text> </View> </TouchableOpacity> </View> </FormelForm> </View> } { let obj=...thisstatedynamicValidateForm; objtype=text; this} { thisrefs'dynamicValidateForm' }
CheckRules
elForm Attributes
parameter | Description | Type | Optional | default | required |
---|---|---|---|---|---|
model | Form data object | object | -- | -- | Y |
canPushChange | Function | -- | -- | N | |
labelWidth | elFormItem label width | Number | --- | -- | N |
styles | Style, covering all styles content | Object | --- | -- | N |
elFormItem Attributes
parameter | Description | Type | Optional | default | required |
---|---|---|---|---|---|
prop | Form field model field, this attribute is required when using validate, resetFields methods | Fields passed into the model of the Form component | Fields passed to the model of the Form component | -- | -- |
label | Label text | string | -- | -- | N |
labelWidth | elFormItem label width | Number | --- | -- | N |
checkOnBlur | Whether to check when losing focus (customInput is true && The input form is a custom input "elFormItem provided by this component") | Boolean | -- | -- | N |
rules | Form validation rules | Array | -- | -- | Y |
elInput
parameter | Description | Type | Optional | default | required |
---|---|---|---|---|---|
errStyle | Successful textInput style | Object | style of successful TextInput | -- | N |
succStyle | failure textInput style | Object | style of failure TextInput | -- | N |
others | Same properties as TextInput | -- | -- | -- | -- |
elForm Methods
Methods | Description | parameter |
---|---|---|
validate | Validate all forms | Function(Array或null) |
validateField | Validate the specified form | Function(Array或null) |
resetField | Reset the form item, reset its value to the initial value, and remove the verification result (because the design of react unidirectional data flow is not easy to implement, it is not provided, you can refer to demo8 to implement, it is not difficult) | -- |
clearValidate | Remove the verification result of this form item |
Warning
- warn
model not contain key:${xxx}
constructor(props) { super(props); this.state={ name:"",//required phone:""//required } } //... setName(){ //Because the required item phone was removed from the state, but Y did not remove the Form.formItem corresponding to phone, which caused the phone to not be removed from the set of validation rules for Form. //If you need to remove a check field, you must remove the corresponding FormItem! !! this.setState({name:"name"}) }