0
votes

I have a Next.js project with Material-UI as the UI framework.

I am doing validation using a React hooks form.

My main component have the form and I have child components for different input fields.

My form component is as shown below:

import { Paper } from "@material-ui/core";
import React from "react";
import { makeStyles, withStyles } from "@material-ui/core/styles";
import CPTextBox from "../../components/Form/CPTextBox";
import { useForm } from "react-hook-form";

const useStyles = makeStyles((theme) => ({
  root: {
    flexGrow: 1,
    "& > *": {
      margin: theme.spacing(1),
    },
  },
  paper: {
    padding: theme.spacing(2),
    textAlign: "center",
    color: theme.palette.text.secondary,
  },
}));

function create2() {
  const classes = useStyles();
  const { control, register, handleSubmit } = useForm();
  const onSubmit = (data) => {
    console.log(data);
  };

  return (
    <Paper className={classes.paper}>
      <form onSubmit={handleSubmit(onSubmit)}>
        <CPTextBox
          attributes={{
            name: "question",
            id: "text_box_1",
            label: "Enter the Question",
            disabled: false,
            type: "",
            fullWidth: true,
            control: { control },
          }}
        />
        <input type="submit" />
      </form>
    </Paper>
  );
}

export default create;

The CPTextBox Component is like this:


import React, { useState } from "react";
import InputLabel from "@material-ui/core/InputLabel";
import FormControl from "@material-ui/core/FormControl";
import { makeStyles, useTheme } from "@material-ui/core/styles";
import { Grid, TextField } from "@material-ui/core";
import { Controller } from "react-hook-form";

const useStyles = makeStyles((theme) => ({
  formControl: {
    // margin: theme.spacing(1),
    minWidth: 120,
    fullWidth: true,
  },
  textbox: {
    width: "100%",
  },
}));

export default function CPTextBox(props) {
  const classes = useStyles();
  return (
    <FormControl fullWidth variant="outlined" className={classes.formControl}>

      <Controller
        as={TextField}
        name={props.attributes.name}
        control={props.attributes.control}
        defaultValue=""
      />
    </FormControl>
  );
}

Using this code, it always gives an error as shown below:

Server Error
TypeError: Cannot read property 'isReValidateOnBlur' of undefined

What will be the fix for the error?

2

2 Answers

1
votes

The control attribute in attributes should not be the object. It should be control itself:

attributes={{
            name: "question",
            id: "text_box_1",
            label: "Enter the Question",
            disabled: false,
            type: "",
            fullWidth: true,
            control: control,
          }}
0
votes

The problem was in transferring the control object inside the attributes:

<CPTextBox
          attributes={{
            name: "question",
            id: "text_box_1",
            label: "Enter the Question",
            disabled: false,
            type: "",
            fullWidth: true,
            **control: control ,**
          }}
        />