react-native-form-generator icon indicating copy to clipboard operation
react-native-form-generator copied to clipboard

errorText field in addition to helpText

Open peacechen opened this issue 8 years ago • 1 comments

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.

peacechen avatar Dec 07 '16 17:12 peacechen

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.

JodiWarren avatar Feb 06 '17 16:02 JodiWarren