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

react hook form validation controller

              rules={{
                required: {
                  value: required,
                  message: 'This field is required',
                },
                validate: {
                  fileType: (file) =>
                    !required ||
                    validFileTypes.indexOf(file.type) != -1 ||
                    'We only support (pdf, doc, docx, txt, rtf)',
                },
              }}
Comment

PREVIOUS NEXT
Code Example
Javascript :: install vue by CDN 
Javascript :: phaser change background color 
Javascript :: javascript json stringify indented 
Javascript :: mongodb mongoose update an element in an array of objects 
Javascript :: javascript filter array of objects by key 
Javascript :: vue router refresh page not found 
Javascript :: change localhost react 
Javascript :: javascript new date from string dd/mm/yyyy 
Javascript :: Lazy Loading Routes vue 
Javascript :: two array in one js 
Javascript :: change the border of an image js 
Javascript :: how to convert json to javascript object in ajax success 
Javascript :: hide a div in jquery 
Javascript :: how to convert set to a string in js 
Javascript :: numbered occurences in regex 
Javascript :: js math function that returns smallest value 
Javascript :: validationResult is not defined 
Javascript :: vue mapgetters with parameter 
Javascript :: javascript datetime format 
Javascript :: sort arrays according to first array js 
Javascript :: space in string using if in jquery 
Javascript :: upload image to firebase 
Javascript :: how to display array values in javascript 
Javascript :: add parameters ajax request 
Javascript :: axios patch 
Javascript :: reset form input react 
Javascript :: javascript listen array change 
Javascript :: npm run start vs npm start 
Javascript :: hide column in antd table using js / react with conditional rendering 
Javascript :: loadtest node 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =