react-native-form-generator
react-native-form-generator copied to clipboard
errorText field in addition to helpText
The helpText field can be leveraged to show an error message for input validation, as shown in the examples. However this results in a lot of boilerplate code, particularly when every input shows input validation errors. This code is repeated N times for N inputs:
helpText={ ((self)=>{
if(Object.keys(self.refs).length !== 0){
if(!self.refs.checkoutForm.refs.first_name.valid){
return self.refs.checkoutForm.refs.first_name.validationErrors.join("\n");
}
}
})(this)}
We could reduce this boilerplate by introducing an errorText field that automatically does the above check on the valid field and displays errorText accordingly.
I abstracted it into
_validationFeedback(self, myRef) {
if( Object.keys(self.refs).length === 0) {
return;
}
if (self.refs.projectInfo.refs[myRef] && !self.refs.projectInfo.refs[myRef].valid) {
return self.refs.projectInfo.refs[myRef].validationErrors.join("\n");
}
return false;
}
Which could be invoked with
<InputField
ref='field_ref'
label='Field Name'
placeholder='Field placeholder'
validationFunction={[validators.required]}
helpText={(this._validationFeedback)(this, "field_ref")}
/>
Obviously that's not a total fix, and I +1 the original, but it helps reduce boilerplate.