Validation 如何在React native中显示按钮单击错误?
我正在使用“react native material textfield”,它运行良好,但我需要在单击提交按钮时显示空字段的错误。我找了很多,但没有找到任何解决办法 如果验证过程失败,请将错误消息置于您的状态,并在单击“提交”按钮后用消息填充Validation 如何在React native中显示按钮单击错误?,validation,react-native,textfield,Validation,React Native,Textfield,我正在使用“react native material textfield”,它运行良好,但我需要在单击提交按钮时显示空字段的错误。我找了很多,但没有找到任何解决办法 如果验证过程失败,请将错误消息置于您的状态,并在单击“提交”按钮后用消息填充 render(){ return ( <View> <TextField {...props} error={this.state.error} errorColo
render(){
return (
<View>
<TextField
{...props}
error={this.state.error}
errorColor={'red'}
onFocus={() => this.setState({error: ''})}
/>
<Button {...props} />
</View>)}
检查开发人员github存储库中的。根据模块文档和示例,只要每个字段的this.state.errors不是空的,就会显示其错误。因此,您的表单应该如下所示:
class Form extends Component {
// ... Some required methods
onSubmit() {
let errors = {};
['firstname'] // This array should be filled with your fields names.
.forEach((name) => {
let value = this[name].value();
if (!value) {
errors[name] = 'Should not be empty'; // The error message when field is empty
}
});
this.setState({ errors });
}
render() {
let { errors = {}, data } = this.state;
return (
<View>
<TextField
value={data.firstname}
onChangeText={this.onChangeText}
error={errors.firstname}
/>
<Text onPress={this.onSubmit}>Submit</Text>
</View>
);
}
}
我正在使用相同的,但它不工作。