Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

reactjs - Validation is not working.. As this data is passed from array from FormBrowser component

Validation is not working for the data which is generated from an array

    import React from 'react'
    import Button from '@material-ui/core/Button';
    import { Link } from 'react-router-dom'
    import '../common.scss';
    import { useFormik } from "formik";
    import * as Yup from "yup";
    import { FormattedMessage, injectIntl } from "react-intl";
    function FormBrowserG(props) {
      const { intl } = props;
      const peopleArray = props.peopleArrayValue;
      //initial values
      const initialValues = {
        peopleArray :[]
      }
      console.log("Arrays",peopleArray);
    
      //creating the validation schema
    const validationSchema = Yup.object().shape({
      people: Yup.array().of(
        Yup.object().shape({
            name: Yup.string()
                .required('Name is required'),
            gender: Yup.string()
                .email('gender is invalid')
                .required('gender is required')
        })
        )
    });
        //using useFormik
    const formik = useFormik({
      initialValues,
      validationSchema,
      onSubmit:(values) => {
      // console.log(values);
    }
    });
       
    // console.log("props =======",props);
    let value = {};
    if(props) {
      value = {
        numberOfPeopleValue: props.numberOfPeopleValue,
        offeringValue: props.offeringValue,
        ratePerQuantityValue:props.ratePerQuantityValue,
        selectedDateValue: props.selectedDateValue,
        organizationNameValue: props.organizationNameValue,
        peopleArrayValue: props.peopleArrayValue
      }
    }
    // console.log(" props ================== ", props.numberOfPeopleValue );
    
    const people = []
      for (let index = 0; index < props.numberOfPeopleValue; index++) {
        people.push(
          
    <fieldset className="fieldset">
        <legend className="legend">Person</legend>
        <div className="display-flex">
            <div className="formname">
                <label className="gendername">
                    <FormattedMessage id="AUTH.GENDER.LABEL"/>
                </label>
                <select  
                  {...formik.getFieldProps('gender')}
                  className="form-control">
                    <option>Select</option>
                    <option key={10}>Male</option>
                    <option key={20}>Female </option>
                </select>
                  {formik.touched.gender && formik.errors.gender ? (
                <div className="warnings">{formik.errors.gender}</div>
                  ) : null}
            </div>
            <div className="formo">
                <label className="gendername">
                    <FormattedMessage id="AUTH.SIGN_UP.NAME.LABEL"/>
                </label>
                <input 
                    type="text" 
                    id={`name${index}`}
                    className="name" 
                    {...formik.getFieldProps(`name${index}`)}
                    required
                  />
                  {formik.touched.name && formik.errors.name ? (
                <div className="warnings">{formik.errors.name}</div>
                  ) : null}
            </div>
        </div>
    </fieldset>
      )}
    return (
    <>
    <form onSubmit={formik.handleSubmit}>
        <div className="row"> 
          {people}
        </div>
    </form>
    <div className="bofferingbtn">
        <Link to={{ 
          pathname: "/Confirmation", 
          state: value 
        }}>
            <Button variant="contained" type="submit" className="obtn" size="large" color="primary" >
                <FormattedMessage id="AUTH.OFFERING.BUTTON"/>
            </Button>
        </Link>
    </div>undefined</>
    )
    }
    export default injectIntl(FormBrowserG)
question from:https://stackoverflow.com/questions/66062482/validation-is-not-working-as-this-data-is-passed-from-array-from-formbrowser-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...