Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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

import React from "react";
import { useForm } from "react-hook-form";

export default function App() {
  const { register, handleSubmit } = useForm();
  const onSubmit = data => console.log(data);
   
  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input {...register("firstName", { required: true, maxLength: 20 })} />
      <input {...register("lastName", { pattern: /^[A-Za-z]+$/i })} />
      <input type="number" {...register("age", { min: 18, max: 99 })} />
      <input type="submit" />
    </form>
  );
}
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 :: routes in angular 
Javascript :: delete node between indexes node list js 
Javascript :: js extract properties from object 
Javascript :: get nested objects via string 
Javascript :: remove in javascript 
Javascript :: sweetalert2 small icon 
Javascript :: object loop 
Javascript :: add style by classname javascript 
Javascript :: object assign in javascript 
Javascript :: how to perform transaction with sequelize 
Javascript :: js regexp match 
Javascript :: == vs === in javascript 
Javascript :: jquery get search parameter 
Javascript :: inline styling js 
Javascript :: react native cli sdk.dir 
Javascript :: react link vs navlink 
Javascript :: polyfill for call 
Javascript :: find the sum of an attribute in sequelize 
Javascript :: invalid json response body at 
Javascript :: react-chartjs-2 
Javascript :: remove all search engines chrome 
Javascript :: startswith in javascript 
Javascript :: nodemon exclude 
Javascript :: how to access variable from another component in angular 
Javascript :: Number of trailing zeros of N! 
Javascript :: Difference between “ == “ and “ === “ operators. 
Javascript :: jquery get last element with two class name 
Javascript :: react responsive nav bar 
Javascript :: adding parameters to url react router 
Javascript :: window frames js 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =