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> ); }} 
                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" ...
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").
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.
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).
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!
input must be an
objecttype, but the final value was:null. If "null" is intended as an empty value be sure to mark the schema as.nullable()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With