Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

306
Visualizações
How to implement two methodes from validation on a form in the react?

I have a project that is only allowed me to change this file (file below). Other files are complete, for this reason, I do not place these in here (also do not lengthen the code). My question is only about the implementation of two methods from validation in this file.

command:

An error is not displayed during the first focus on input until taken focus from it. If there is an error, it displays. and next times, during of the changing if encounter with an error, the same time displays.

I implemented the first section from the above command, how do I the second section in the same file? With 'useEffect'? How? Thanks for any helping.

input.js

import {useState} from "react";
import {validate} from './validators';

const INPUT_STATES = { /*this is not used!*/
  UNTOUCHED: 'UNTOUCHED',
  VALID: 'VALID',
  INVALID: 'INVALID'
};

const myValidate= validate; /*validate is the my validation function*/

const Input = props => {
   const [value,setValue] = useState('');
   const [validate,setValidate] = useState(true);

   const handleChange = (e) => {
       setValue(e.target.value);
   }

   const handleValidate = () => {
       const Validate = myValidate(value,props.validators);
       setValidate(Validate);
   }

   return (
     <div className={`form-input ${validate ? '' : 'form-input--invalid'}`} data-testid="form-input">
       <label htmlFor={props.id}>{props.label}</label>
       <input value={value} type={props.type} id={props.id} onChange={handleChange} onBlur={handleValidate} />
       <p>{validate ? '' : props.errorText}</p>
     </div>
   )
};

export default Input;

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I solved the above problem, by adding two If inside handleChange and handleValidate. Also, I defined another useState.

Also, I changed first parameter from myValidate, from value to e.target.value.

input.js:

import React from "react";
import {useState} from "react";
import {validate} from './validators';/*This file is cantains validation rulles.*/

const myValidate = validate; /*validate is the my validation function*/
const Input = props => {
   const [value,setValue] = useState('');
   const [validate,setValidate] = useState(true);
   const [valid,setValid] = useState('VALID');

   const handleChange = (e) => {
       setValue(e.target.value);
       if(valid === 'INVALID'){
           handleValidate(e);
       }
   }
   const handleValidate = (e) => {
       const Validate = myValidate(e.target.value,props.validators);
       setValidate(Validate);
       if(!Validate){
           setValid('INVALID');
       }
   }
   return (
     <div className={`form-input ${validate ? '' : 'form-input--invalid'}`} data-testid="form-input">
       <label htmlFor={props.id}>{props.label}</label>
       <input value={value} type={props.type} id={props.id} onChange={handleChange} onBlur={handleValidate} />
       <p>{validate ? '' : props.errorText}</p>
     </div>
   )
};

export default Input;

Now:

An error is not displayed during the first focus on input until taken focus from it. If there is an error, it displays. and next times, during of the changing if encounter with an error, the same time displays.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda