Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
Use default Material-UI validation in form submit

I'm trying to implement a form with validation:

import React from 'react';

import { useForm } from "react-hook-form";
import axios, {AxiosResponse} from "axios";
import {Box, Button, Container, Grid, Typography} from "@material-ui/core";
import TextField from '@material-ui/core/TextField';

export async function postTicket(data: TicketDTO): Promise<AxiosResponse<TicketDTO[]>> {
  return await axios.post<TicketDTO[]>(
      `http://localhost:8080/api/support/tickets/create`
  );
}

export interface TicketDTO {
  title?: string;
}

export default function OpenTicket(props: any) {
  const {
      register,
      handleSubmit,
      formState: { errors }
  } = useForm<TicketDTO>();

  const onSubmit = async (data: TicketDTO) => {
      try {
          await postTicket(data);
      } catch (err) {
          console.log(err);
      }
  };

  return (
      <Container>
           <form onSubmit={handleSubmit(onSubmit)}>
               <TextField
                   id="outlined-full-width"
                   label="Name"
                   {...register("title", { required: true, maxLength: 20 })}
                   placeholder="Placeholder"
                   helperText="Full width!"
                   fullWidth
                   margin="normal"
                   InputLabelProps={{
                       shrink: true,
                   }}
                   variant="outlined"
               />

              <Button
                  variant="contained"
                  color="primary"
                  size="small"
              >
                  Submit
              </Button>
          </form>
      </Container>
  );
}

Sandbox: https://stackblitz.com/edit/react-ts-4pnf8b?file=Hello.tsx

But when I click submit button nothing happens. I would like to use the Materia-ui default validation messages? Do you know where I'm wrong?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I have made some modifications to your code,

  1. Added type to your button as submit
  2. required can be directly added to Text-Field.
  3. maxLength can be applied as part of inputProps.

I am not quite sure what you are doing in your submit event. Ignoring for now.

https://stackblitz.com/edit/react-ts-dufrzd?file=Hello.tsx

<form onSubmit={handleSubmit(onSubmit)}>
        <TextField
          id="outlined-full-width"
          label="Name"
          required          
          placeholder="Placeholder"
          helperText="Full width!"
          fullWidth
          margin="normal"
          InputLabelProps={{
            shrink: true,
          }}
          inputProps={{maxLength:20}}
          variant="outlined"
        />

        <Button variant="contained" color="primary" size="small" type="submit">
          Submit
        </Button>
      </form>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!