Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react hook form with yup resolver

import { yupResolver } from '@hookform/resolvers/yup';
import { useForm } from 'react-hook-form';

const useValidationForm = (schema: any) => {
	const { ...props } = useForm({
		mode: 'onChange',
		resolver: yupResolver(schema),
	});

	return {
		...props,
	};
};

export default useValidationForm;
Comment

select react hook form yup validation

import React from "react";
import ReactDOM from "react-dom";
import { useForm } from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
import * as yup from "yup";

import "./styles.css";

const SignupSchema = yup.object().shape({
  select: yup.string().required()
});

function App() {
  const {
    register,
    setValue,
    handleSubmit,
    formState: { errors }
  } = useForm({
    mode: "onChange",
    resolver: yupResolver(SignupSchema)
  });
  const onSubmit = (data) => {
    alert(JSON.stringify(data));
  };

  const doSomething = async (value) => {
    // do something with my select value onChange
  };

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <div>
        <label>Select</label>
        <select
          {...register("select")}
          onChange={(e) => setValue('select', e.target.value, { shouldValidate: true })} // Using setValue
        >
          <option value="">Null</option>
          <option value="1">1</option>
          <option value="2">2</option>
        </select>
        {errors.select && <p>{errors.select.message}</p>}
      </div>
      <input type="submit" />
    </form>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Comment

PREVIOUS NEXT
Code Example
Javascript :: chai compare arrays 
Javascript :: converting a string into a number in javascript 
Javascript :: number constructor js 
Javascript :: adding to array using reach hooks 
Javascript :: text field material ui max input for number 
Javascript :: javascript download current html page 
Javascript :: url params vue 
Javascript :: regex to check if text is in quotations 
Javascript :: javascript wait async 
Javascript :: stop interval js 
Javascript :: moment time format by country 
Javascript :: javascript blob to file 
Javascript :: jquery navigation 
Javascript :: array sort by two properties 
Javascript :: lorem ipsum json api 
Javascript :: cdn jquery 
Javascript :: Obtain smallest value from array of objects in Javascript 
Javascript :: count json objects 
Javascript :: leaflet each layer 
Javascript :: get promise result from json() javascript 
Javascript :: css canvas set aspect ratio 
Javascript :: binary search algorithm in javascript 
Javascript :: how to filter out undefined keys from object in js 
Javascript :: how to know if ajax is running 
Javascript :: adding attribute in jquery 
Javascript :: javascript format date time 
Javascript :: axios cancel request 
Javascript :: what is synchronous and asynchronous in javascript 
Javascript :: json objects 
Javascript :: react array.map with return 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =