Skip to content

Commit

Permalink
Exibindo erros no Input
Browse files Browse the repository at this point in the history
  • Loading branch information
danilo-vieira committed Oct 19, 2020
1 parent 5b3f78f commit b943424
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions nivel-03/02-iniciando-o-front-end-web/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"react/prop-types": "off",
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn",
"no-unused-expressions": "off",
"react/jsx-filename-extension": [1, { "extensions": [".tsx"] }],
"import/prefer-default-export": "off",
"no-use-before-define": "off",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const Input: React.FC<InputProps> = ({ name, icon: Icon, ...rest }) => {
{...rest}
type="text"
/>
{error}
</Container>
);
};
Expand Down
19 changes: 15 additions & 4 deletions nivel-03/02-iniciando-o-front-end-web/src/pages/SignUp/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import React, { useCallback } from 'react';
import React, { useCallback, useRef } from 'react';
import { FiArrowLeft, FiUser, FiMail, FiLock } from 'react-icons/fi';
import { FormHandles } from '@unform/core';
import { Form } from '@unform/web';
import * as Yup from 'yup';

import getValidationErrors from '../../utils/getValidationErrors';

import logoImg from '../../assets/logo.svg';

import Input from '../../components/Input';
Expand All @@ -11,8 +14,12 @@ import Button from '../../components/Button';
import { Container, Content, Background } from './styles';

const SignUp: React.FC = () => {
const formRef = useRef<FormHandles>(null);

const handleSubmit = useCallback(async (data: object) => {
try {
formRef.current?.setErrors({});

const schema = Yup.object().shape({
name: Yup.string().required('Nome obrigatório'),
email: Yup.string()
Expand All @@ -25,7 +32,11 @@ const SignUp: React.FC = () => {
abortEarly: false,
});
} catch (err) {
console.log(err);
if (err instanceof Yup.ValidationError) {
const errors = getValidationErrors(err);

formRef.current?.setErrors(errors);
}
}
}, []);

Expand All @@ -35,10 +46,10 @@ const SignUp: React.FC = () => {
<Content>
<img src={logoImg} alt="GoBarber" />

<Form onSubmit={handleSubmit}>
<Form ref={formRef} onSubmit={handleSubmit}>
<h1>Faça seu cadastro</h1>

<Input name="name" icon={FiUser} placeholder="E-mail" />
<Input name="name" icon={FiUser} placeholder="Nome" />

<Input name="email" icon={FiMail} placeholder="E-mail" />

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ValidationError } from 'yup';

interface Errors {
[key: string]: string;
}

export default function getValidationErrors(err: ValidationError): Errors {
const validationErrors: Errors = {};

err.inner.forEach(error => {
validationErrors[error.path] = error.message;
});

return validationErrors;
}

5 comments on commit b943424

@wilsonfaustino
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recebi na linha 11 de [nivel-03/02-iniciando-o-front-end-web/src/utils/getValidationErrors.ts ] o erro de que 'undefined' não poderia ser usado como index type.
Corrigi usando:

err.inner.forEach(error => { if (error.path) validationErrors[error.path] = error.message; });

Espero que isso possa ajudar alguém.

@fernandogostack
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recebi na linha 11 de [nivel-03/02-iniciando-o-front-end-web/src/utils/getValidationErrors.ts ] o erro de que 'undefined' não poderia ser usado como index type.
Corrigi usando:

err.inner.forEach(error => { if (error.path) validationErrors[error.path] = error.message; });

Espero que isso possa ajudar alguém.

Me ajudou. Muito obrigado!

@JoaoCapoAlm
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recebi na linha 11 de [nivel-03/02-iniciando-o-front-end-web/src/utils/getValidationErrors.ts ] o erro de que 'undefined' não poderia ser usado como index type.
Corrigi usando:

err.inner.forEach(error => { if (error.path) validationErrors[error.path] = error.message; });

Espero que isso possa ajudar alguém.

Acabou me ajudando tbm, estava dando erro para mim

@danilo-vieira
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wilsonfaustino obrigado por esse feedback! Irei adicionar isso na descrição da aula 💜

@ttasca
Copy link

@ttasca ttasca commented on b943424 Apr 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ajudou muito @wilsonfaustino muito obrigado.

Please sign in to comment.