如何在React Native中的注册表单中显示错误消息?

huangapple go评论131阅读模式
英文:

How to show error message in registerform with react-native?

问题

To use the handleValidEmail method with the input email field in your React Native app, you should call this method when the user interacts with the email input field. Specifically, you should call it in the onChangeText prop of your email input field in the RegisterScreen component.

Here's how you can modify the code to use handleValidEmail:

<AuthInput
    label="E-mail"
    value={email}
    textContentType="emailAddress"
    keyboardType="email-address"
    autoCapitalize="none"
    onChangeText={(u) => {
        setEmail(u);
        handleValidEmail(u); // Call handleValidEmail when the email input changes
    }}
    error={emailValidError}
/>

By adding handleValidEmail(u); inside the onChangeText prop, you are invoking the handleValidEmail function with the current value of the email input whenever the user types or modifies the email address. This allows you to perform email validation and update the emailValidError state accordingly.

英文:

I have a react-native app. And I try to show specific error message to the user when email is not correct formatted.

this is the context with the email validation:

export const AuthContextProvider = ({ children }) =&gt; {
	const [isLoading, setIsLoading] = useState(false);
	const [error, setError] = useState(null);
	const [user, setUser] = useState(null);
	const [emailValidError, setEmailValidError] = useState(&quot;&quot;);



	const handleValidEmail = (val) =&gt; {
		let reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w\w+)+$/;

		if (val.length === 0) {
			setEmailValidError(&quot;email address must be enter&quot;);
		} else if (reg.test(val) === false) {
			setEmailValidError(&quot;enter valid email address&quot;);
		} else if (reg.test(val) === true) {
			setEmailValidError(&quot;&quot;);
		}
	};

	const onRegister = async (email, username, password, password2) =&gt; {
		
		if (password !== password2) {
			setError(&quot;wachtwoorden zijn niet gelijk aan elkaar&quot;);
			return;
		}
		registerRequest(email, username, password, password2)
			.then((u) =&gt; {
				setUser(u);
				setIsLoading(false);
			})
			.catch((e) =&gt; {
				setIsLoading(false);
				setError(e.toString());
			});
	};

	return (
		&lt;AuthContext.Provider
			value={{
				isAuthenticated: !!user,
				isLoading,
				error,
				user,
				onLogin,
				onRegister,
				handleValidEmail,
			}}&gt;
			{children}
		&lt;/AuthContext.Provider&gt;
	);
};

And this is the register from:

		export const RegisterScreen = ({ navigation }) =&gt; {
	const [email, setEmail] = useState(&quot;&quot;);
	const [username, setUsername] = useState(&quot;&quot;);
	const [password, setPassword] = useState(&quot;&quot;);
	const [password2, setPassword2] = useState(&quot;&quot;);
	const { onRegister, isLoading, error, handleValidEmail, emailValidError } =
		useContext(AuthContext);

	return (
		&lt;AccountBackground&gt;
			&lt;AccountCover /&gt;
			&lt;Title&gt; app&lt;/Title&gt;
			&lt;AccountContainer&gt;
				&lt;AuthInput
					label=&quot;E-mail&quot;
					value={email}
					textContentType=&quot;emailAddress&quot;
					keyboardType=&quot;email-address&quot;
					autoCapitalize=&quot;none&quot;
					onChangeText={(u) =&gt; {
						setEmail(u);
						handleValidEmail(u);
					}}
					error={emailValidError}
				/&gt;

				&lt;Spacer size=&quot;large&quot;&gt;
					&lt;AuthInput
						label=&quot;username&quot;
						value={username}
						textContentType=&quot;username&quot;
						keyboardType=&quot;username&quot;
						autoCapitalize=&quot;none&quot;
						onChangeText={(user) =&gt; setUsername(user)}
					/&gt;
				&lt;/Spacer&gt;
				&lt;Spacer size=&quot;large&quot;&gt;
					&lt;AuthInput
						label=&quot;Wachtwoord&quot;
						value={password}
						textContentType=&quot;password&quot;
						secureTextEntry
						autoCapitalize=&quot;none&quot;
						onChangeText={(p) =&gt; setPassword(p)}
					/&gt;
				&lt;/Spacer&gt;
				&lt;Spacer size=&quot;large&quot;&gt;
					&lt;AuthInput
						label=&quot;Herhaal wachtwoord&quot;
						value={password2}
						textContentType=&quot;password2&quot;
						secureTextEntry
						autoCapitalize=&quot;none&quot;
						onChangeText={(pas) =&gt; setPassword2(pas)}
					/&gt;
				&lt;/Spacer&gt;
				{error &amp;&amp; (
					&lt;ErrorContainer size=&quot;large&quot;&gt;
						&lt;Text variant=&quot;error&quot;&gt;{error}&lt;/Text&gt;
					&lt;/ErrorContainer&gt;
				)}
				&lt;Spacer size=&quot;large&quot;&gt;
					{!isLoading ? (
						&lt;AuthButton
							icon=&quot;email&quot;
							mode=&quot;contained&quot;
							onPress={() =&gt; onRegister(email, username, password, password2)}&gt;
							Register
						&lt;/AuthButton&gt;
					) : (
						&lt;ActivityIndicator animating={false} /&gt;
					)}
				&lt;/Spacer&gt;
			&lt;/AccountContainer&gt;
			&lt;Spacer size=&quot;large&quot;&gt;
				&lt;AuthButton mode=&quot;contained&quot; onPress={() =&gt; navigation.goBack()}&gt;
					Back
				&lt;/AuthButton&gt;
			&lt;/Spacer&gt;
		&lt;/AccountBackground&gt;
	);
};

and errorContainer:

export const ErrorContainer = styled.View`
	max-width: 300px;
	align-items: center;
	align-self: center;
	margin-top: ${(props) =&gt; props.theme.space[2]};
	margin-bottom: ${(props) =&gt; props.theme.space[2]};
`;

export const AuthInput = styled(TextInput)`
	width: 200px;
	background-color: #c6daf7;
`;

But when the email textinput left blanco. it only shows error.

Question: how to use the method handleValidEmail with the input email field?

答案1

得分: 1

Your code doesn't show where the error state should be displayed. You set the state but you are not using it in your view.

You can do that by modifying the AuthInput to accept the error like the following:

<AuthInput
    label="E-mail"
    value={email}
    textContentType="emailAddress"
    keyboardType="email-address"
    autoCapitalize="none"
    onChangeText={(u) => {
        setEmail(u);
        handleValidEmail(u);
    }}
    error={emailValidError}
/>

And display the text in the AuthInput component:

/* Somewhere: */
{error ? <Text>{error}</Text> : null}
英文:

Your code doesn't show where the error state should be displayed. You set the state but you are not using it in your view.

You can do that by modifying the AuthInput to accept the error like the following:

&lt;AuthInput
    label=&quot;E-mail&quot;
    value={email}
    textContentType=&quot;emailAddress&quot;
    keyboardType=&quot;email-address&quot;
    autoCapitalize=&quot;none&quot;
    onChangeText={(u) =&gt; {
        setEmail(u);
        handleValidEmail(u);
    }}
    error={emailValidError}
/&gt;

And display the text in the AuthInput component:

/* Somewhere: */
{error ? &lt;Text&gt;{error}&lt;/Text&gt; : null}

huangapple
  • 本文由 发表于 2023年5月14日 19:36:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76247261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定