Skip to content

I'm unable to change the validation rules for the password field in the users collection. #11982

Closed

Description

@MDrzal1886

Describe the Bug

I want to use the default authentication method in the payload, but I also need to set custom validations on the password field. However, I don't see any way to do this since I don't have direct access to the password field. By default, the only validations applied are required and a minimum length of 3 characters.

At the very least, there should be an option to adjust the minimum character requirement (e.g., increasing it from 3 to 8). Additionally, the ability to define custom validation rules for passwords would be highly beneficial.

Link to the code that reproduces this issue

https://github.com/payloadcms/payload/tree/main/templates/website

Reproduction Steps

Run the code from the template or demo version and try to modify the validation rules for the password field.

export const Users: CollectionConfig = {
  slug: 'users',
  access: {
    admin: authenticated,
    create: authenticated,
    delete: authenticated,
    read: authenticated,
    update: authenticated,
  },
  admin: {
    defaultColumns: ['name', 'email'],
    useAsTitle: 'name',
  },
  auth: true,
  fields: [
    {
      name: 'name',
      type: 'text',
    },
  ],
  timestamps: true,
}

Which area(s) are affected? (Select all that apply)

area: core

Environment Info

Payload: 3.31.0
Node.js: 20.15.1
Next.js: 15.2.3

Activity

  1. added
    status: needs-triage
    validate-reproduction
    on Apr 4, 2025
  2. removed
    validate-reproduction
    on Apr 4, 2025
  3. self-assigned this
    on Apr 8, 2025
  4. removed
    status: needs-triage
    on Apr 8, 2025
  5. JessRynkar commented on Apr 8, 2025

    @JessRynkar
    Member

    Hi @MDrzal1886 - you can set up custom validation for the password field using the beforeValidate hook on your users collection. Here's an example of how to add it and apply the validation:

    import { ValidationError } from 'payload' // use this to throw the error on the password field
    
    export const Users: CollectionConfig = {
      slug: 'users',
      auth: true,
      hooks: {
            beforeValidate: [
              ({ data: { password } = {} }) => {
                if (!password) {
                  return
                }
    
                let message: string | undefined
    
                if (password.length < 8) {
                  message = 'Password must be at least 8 characters long.'
                }
    
                // you can add more conditions and update the error message as needed
                const hasSymbols = /[$-/:-?{-~!"^_`[\]]/.test(password)
                if (!hasSymbols) {
                  message = 'Password must include at least one symbol.'
                }
    
                if (message) {
                  throw new ValidationError({
                    collection: 'users',
                    errors: [{ message, path: 'password' }],
                  })
                }
              },
            ],
          },
      // ... rest of config
    }
    

    The output of this validation would look like this:
    Image

    Hope this helps!

  6. MDrzal1886 commented on Apr 9, 2025

    @MDrzal1886
    Author

    @JessChowdhury
    Thanks a lot, but I still have two issues with this solution:

    • When I enter only two characters, I see a “minimum 3 characters” validation message, which I believe is incorrect behavior, since the validation should require a minimum of 8 characters.

    Image

    • The validation only runs when the button is clicked, not while typing.

    Image

  7. github-actions commented on Apr 17, 2025

    @github-actions
    Contributor

    This issue has been automatically locked.
    Please open a new issue if this issue persists with any additional detail.

  8. locked as resolved and limited conversation to collaborators on Apr 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions