feat: add form validation on sign up screen

This commit is contained in:
Vinicius Souza 2024-10-28 14:33:26 -03:00
parent c94c437ec7
commit 08f81b743d
2 changed files with 61 additions and 32 deletions

View file

@ -1,12 +1,28 @@
import { Input as GSInput, InputField } from '@gluestack-ui/themed'; import {
FormControl,
FormControlError,
FormControlErrorText,
Input as GSInput,
InputField,
} from '@gluestack-ui/themed';
import { ComponentProps } from 'react'; import { ComponentProps } from 'react';
type Props = ComponentProps<typeof InputField> & { type Props = ComponentProps<typeof InputField> & {
isReadOnly?: boolean; isReadOnly?: boolean;
errorMessage?: string | null;
isInvalid?: boolean;
}; };
export function Input({ isReadOnly = false, ...others }: Props) { export function Input({
isReadOnly = false,
errorMessage = null,
isInvalid = false,
...others
}: Props) {
const invalid = !!errorMessage || isInvalid;
return ( return (
<FormControl w="$full" mb="$4" isInvalid={invalid}>
<GSInput <GSInput
h="$14" h="$14"
borderColor="$gray700" borderColor="$gray700"
@ -26,5 +42,10 @@ export function Input({ isReadOnly = false, ...others }: Props) {
{...others} {...others}
/> />
</GSInput> </GSInput>
<FormControlError>
<FormControlErrorText color="$red500">{errorMessage}</FormControlErrorText>
</FormControlError>
</FormControl>
); );
} }

View file

@ -20,15 +20,11 @@ type FormDataProps = {
export function SignUp() { export function SignUp() {
const navigation = useNavigation<AuthNavigatorRoutesProps>(); const navigation = useNavigation<AuthNavigatorRoutesProps>();
const { control, handleSubmit } = useForm<FormDataProps>({ const {
// unnecessary because this is a registration screen control,
defaultValues: { handleSubmit,
name: '', formState: { errors },
email: '', } = useForm<FormDataProps>();
password: '',
confirmPassword: '',
},
});
function handleSignUp({ name, email, password, confirmPassword }: FormDataProps) { function handleSignUp({ name, email, password, confirmPassword }: FormDataProps) {
console.debug({ console.debug({
@ -69,12 +65,15 @@ export function SignUp() {
<Controller <Controller
control={control} control={control}
name="name" name="name"
render={({ field: { onChange, value } }) => ( rules={{ required: 'Informe o nome.' }}
render={({ field: { onBlur, onChange, value } }) => (
<Input <Input
placeholder="Nome" placeholder="Nome"
autoCorrect={false} autoCorrect={false}
onBlur={onBlur}
onChangeText={onChange} onChangeText={onChange}
value={value} value={value}
errorMessage={errors.name?.message}
/> />
)} )}
/> />
@ -82,14 +81,23 @@ export function SignUp() {
<Controller <Controller
control={control} control={control}
name="email" name="email"
render={({ field: { onChange, value } }) => ( rules={{
required: 'Informe o e-mail.',
pattern: {
value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i,
message: 'E-mail inválido',
},
}}
render={({ field: { onBlur, onChange, value } }) => (
<Input <Input
placeholder="E-mail" placeholder="E-mail"
keyboardType="email-address" keyboardType="email-address"
autoCapitalize="none" autoCapitalize="none"
autoCorrect={false} autoCorrect={false}
onBlur={onBlur}
onChangeText={onChange} onChangeText={onChange}
value={value} value={value}
errorMessage={errors.email?.message}
/> />
)} )}
/> />