我有一个很好的注册表单,很好!但我想创建一个布尔字段:“terms”,我希望当它被点击时,它允许你验证表单。但是当没有点击时,你不能注册。我不知道我怎么能做到这一点。
我的想法是设定一个条件“如果条款被接受,那么 => 表格已经过验证,您可以进入您的帐户”。在我看来,这是正确的方法,但我不知道我该怎么做。也许像我为电子邮件、电话和密码创建一个 const 条款,但如何写它必须验证才能注册?也许在该州有“接受错误”之类的东西,但我不知道如何在“条款”字段中传递“接受”选项......
谢谢你的帮助。
import React, { Component } from "react";
import {
ScrollView,
View,
StyleSheet,
Text,
ImageBackground,
AsyncStorage
} from "react-native";
import styles from "../../assets/Styles/Styles";
import i18n from "../../src/i18n";
import { storeProfileToken } from "../../src/common/util/MyPreferences";
import Button from "react-native-flat-button";
import {
API_URL,
API_SECRETKEY
} from "../../src/common/util/Constants";
import t from "tcomb-form-native";
import stylesheet from "../../assets/Styles/FormStyles";
import base64 from 'react-native-base64'
const Form = t.form.Form;
const Email = t.refinement(t.String, email => {
const reg = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/; //or any other regexp
return reg.test(email);
});
const Phone = t.refinement(t.maybe(t.String), phone_number => {
const reg = /^(?:\+\d{1,3}|0\d{1,3}|00\d{1,2})?(?:\s?\(\d+\))?(?:[-\/\s.]|\d)+$/; //or any other regexp
return reg.test(phone_number);
});
const Pwd = t.refinement(t.String, password => {
const reg = /^(?=.{8,})/; //or any other regexp
return reg.test(password);
});
const User = t.struct({
email: Email,
password: Pwd,
phone_number: Phone,
terms: t.Boolean
});
const formStyles = {
...Form.stylesheet
};
const options = {
fields: {
email: {
label: i18n.t("signup.input.email"),
error: i18n.t("signup.error.email")
},
password: {
password: true,
secureTextEntry: true,
label: i18n.t("signup.input.password"),
error: i18n.t("signup.error.password")
},
phone_number: {
label: i18n.t("signup.input.phone"),
error: i18n.t("signup.error.phone")
},
terms: {
label: i18n.t("signup.input.terms"),
error: i18n.t("signup.error.terms")
}
},
stylesheet: stylesheet
};
export default class Signup extends Component {
constructor(props) {
super(props);
this.state = {
showProgress: null,
email: "",
password: "",
phone_number: "",
error: "",
accepted :false
};
}
async onRegisterPressed(email, pwd, phone, term) {
//console.log("SIGNUP::email: ", email);
//console.log("SIGNUP::email: ", pwd);
//console.log("SIGNUP::email: ", phone);
//console.log("SIGNUP::email: ", term);
if (email != "" && pwd != "") {
this.setState({ showProgress: true });
try {
let response = await fetch(
API_URL +
"/users?society_id=131&access_token=" +
"accessToken" +
"&lang=fr",
{
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: "Bearer " + API_SECRETKEY
},
body: JSON.stringify({
email: email,
password: pwd,
mobilephone: phone
})
}
)
.then(response => response.json())
.then(responseData => {
//console.log("YOU HAVE SUCCESFULLY LOGGED IN:", responseDocs)
console.log("ok: ", responseData.status);
if (responseData.status >= 200 && responseData.status < 300) {
console.log("data: ", responseData.data);
//Handle success
let accessToken = responseData;
console.log(accessToken);
//On success we will store the access_token in the AsyncStorage
this.storeProfileToken(accessToken);
this.redirect(MyTrips);
} else {
//Handle error
console.log("data-error: ", responseData.data.error);
let error = responseData;
throw responseData.data.error;
}
});
return result;
} catch (error) {
this.setState({ error: error });
console.log("error " + error);
this.setState({ showProgress: false });
}
}
}
handleSubmit = async () => {
const data = this._form.getValue();
//console.log("SIGNUP::data: ", data);
if (data && data.email && data.password && data.terms) {
this.onRegisterPressed(
data.email,
data.password,
data.phone_number,
data.terms
);
} /*else if (terms = ok) {
this.setState({accepted:true})
} else {this.props.navigation.navigate("Authentication")}*/
};
render() {
return (
<ImageBackground
source={require("../../assets/images/bg_mobile_paysage.jpg")}
style={{ flex: 1 }}
>
<ScrollView>
<View style={styles.container}>
<Text style={styles.h5}>{"\n"}</Text>
<Text style={styles.h1}>{i18n.t("signup.title")}</Text>
<Text style={styles.h5}>{"\n"}</Text>
<Form ref={c => (this._form = c)} type={User} options={options} />
<Button
containerStyle={[styles.mybtnContainer]}
style={styles.mybtn}
onPress={this.handleSubmit}
>
{i18n.t("signup.action.subscribe").toUpperCase()}
</Button>
<Button
onPress={() => this.props.navigation.navigate("Login")}
containerStyle={[styles.mybtnContainer, styles.btnContainerMuted]}
style={styles.mybtn}
>
{i18n.t("signup.action.haveAccount").toUpperCase()}
</Button>
</View>
</ScrollView>
</ImageBackground>
);
}
}