Payloadcms validation bug

499 Views Asked by At

I'm using Payloadcms 1.11.2 i'm trying to validate a field with number type using Regexp it seems that the validation function does not work correctly since it show error message for correct values this happen when i re-click the save button more then one time

const card: GlobalConfig = {
  slug: "card",
  fields: [
    {
      type: "tabs",
      tabs: [
        {
          label: "labels",
          fields: [
            {
              type: "text",
              name: "explore",
              required: true,
              localized: true,
            },
            {
              type: "text",
              name: "other",
              required: true,
              localized: true,
            },
          ],
        },
        {
          label: "info",
          fields: [
            {
              type: "number",
              name: "issue_year",
              label: "year of issue",
              required: true,
              validate: (val, args) => {
                const pattern = /20[2-9][/\d]/g;

                if (val) {
                  if (pattern.test(val.toString())) {
                    return true;
                  } else {
                    return "invalid year of issue";
                  }
                } else {
                  return true;
                }

              },
            },
          ],
        },
      ],
    },
  ],
};

Any help please

1

There are 1 best solutions below

0
On BEST ANSWER

I tried to recreate your issue, but it appears to validate as expected for me.

  • ✅ 2023
  • ✅ 2099
  • ❌ 1999
  • ❌ 2001
  • ❌ 2100

The following code works for me:

{
  type: 'number',
  name: 'issue_year',
  label: 'year of issue',
  required: true,
  validate: (val) => {
    if (!val) return true;
    const pattern = /20[2-9][/\d]/g;
    return pattern.test(val.toString()) || 'invalid year of issue';
  },
}