Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign object validation in yup for react-select (single-select)

I am trying to implement validation for react-select (single-select) using yup concept. But i am getting this error:

Objects are not valid as a React child (found: object with keys {label, value}). If you meant to render a collection of children, use an array instead.

I want to know how to assign objects in validation schema for yup concept

<Formik
  initialValues={{
    department:"",
  }}
  onSubmit={this.submitForm}
  validationSchema={Yup.object().shape({
    department: Yup.object().shape({
      label: Yup.string().required(),
      value: Yup.string().required(),
    }),
})}>
{ props => {
  const {
    values,
    touched,
    errors,
    isSubmitting,
    handleChange,
    handleBlur,
    handleSubmit,
    selectedOption
  } = props;
  return (
    <React.Fragment>

    <InputLabel shrink htmlFor={'department'}>
    Department</InputLabel>
    <Select
          inputId="department"
          options={options}
          value={values.department}
          onChange={this.onChangeOption}
          onBlur={handleBlur}         
       />
{errors.department && touched.department && ( {errors.department} )} 
Submit </div> </React.Fragment> ); }} 
like image 728
Taruni Avatar asked Feb 28 '19 10:02

Taruni


People also ask

How do you validate react select dropdown in react?

const options = [ { value: 'active', label: 'Active' }, { value: 'inactive', label: 'In Active' }, { value: 'deleted', label: 'Delete' }, ]; <Select defaultValue={options[0]} isSearchable={false} className="react-dropdown" onChange={statusDropdownHandleChange} classNamePrefix="dropdown" options={options} name="status" ...

How use Yup validation in react?

import React from "react"; import * as Yup from "yup"; const ProductReviewForm = () => { const products = ["Product 1", "Product 2", "Product 3", "Product 4"]; const validationSchema = Yup. object({ product: Yup. string(). required("Please select a product").

How do you make a select required in react?

In this article, we will see how to solve How We Can Set React Select Required with examples. import React from "react"; import { Form, Select, Button } from "antd"; import "./styles. css"; import "antd/dist/antd. css"; const { Option } = Select; const AppWithForm = props => { const { getFieldDecorator } = props.


2 Answers

I want to know how to assign objects in validation schema for yup concept

You did it correct (as far as I can tell):

validationSchema={Yup.object().shape({
  department: Yup.object().shape({
    label: Yup.string().required(),
    value: Yup.string().required(),
  })
}

What React complains about is this line:

{errors.department && touched.department && ( {errors.department} )} 

What this evalautes to is if errors has a key called department and if touched has a key called department then render the object errors.department. React can't do that. If you want to display the errors you I suggest having a dedicated component (e.g. a <p> tag) for it outside of your select. Something like this:

{errors.department && touched.department && (<p>{errors.department.label}</p>)}

You can do something similar for value.

PS: Your code seems incomplete and poorly formatted (e.g. there is a floating <div /> tag).

like image 58
J. Hesters Avatar answered Sep 20 '22 14:09

J. Hesters


You can refine the shape schema with nullable() and required().

Example

const requiredOption = Yup.object()
  .shape({
    value: Yup.string(),
    label: Yup.string(),
  })
  .nullable()
  .required('This field is required.');

Why nullable() and required() to the shape!

  1. If an input is focused and the user decides to skip the input, a value for that input is still set to null and the validation is triggered. Eventually, you would end up with an error like this.

input must be an object type, but the final value was: null. If "null" is intended as an empty value be sure to mark the schema as .nullable()

  1. Instead of having required on individual properties of shape schema, if you are sure all the properties are mandatory, the required function can be called on the object shape directly.
like image 39
Deepak Kadarivel Avatar answered Sep 18 '22 14:09

Deepak Kadarivel