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

136
Views
Antd Prints 2 error together for custom validator with pre-defined rules

I am using an antd text area in reactjs, and have some predefined rules as :

    const validationRules = [
        {
            required: true,
            message: 'Required',
        },
        {
            min: MIN_LENGTH,
            message: `min ${MIN_LENGTH} characters required`,
        },
    ];

Form Item is as follows:

<Form.Item name="myField" rules={validationRules}>
  <TextArea
    className="mt-32"   
    rows={6}
    placeholder={`Your answer should be between ${MIN_LENGTH} to ${MAX_LENGTH} characters`}
    minLength={MIN_LENGTH}
    maxLength={MAX_LENGTH}
    value={value}
  />
</Form.Item>;

Now want to add a custom validation rule i.e. to warn user if he enters more than 2 tailing spaces.
For this I have created a custom validator as:

async function validateEmptySpaces(rule: any, inputValue: string) {
  const trimmedValue = inputValue.trim();
  const diffInLength = inputValue.length - trimmedValue.length;
  if (diffInLength > 1) {
    throw new Error("remove empty spaces");
  }
}

So I add this validator to mu rules at the end as :

const validationRules = [
  {
    required: true,
    message: "Required",
  },
  {
    min: MIN_LENGTH,
    message: `min ${MIN_LENGTH} characters required`,
  },
  { validator: validateEmptySpaces },
];

Now this is How I get to see in UI enter image description here

Here instead of 1 error at a time, it shows both the errors. How can I make it show only one error message at once like: enter image description here

about 4 years ago · Juan Pablo Isaza
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!