Melchyore / adonis-form-request

Use dedicated classes to authorize and validate requests
MIT License
23 stars 1 forks source link

Improve docs by adding examples on how to use custom validation messages #1

Open segungreat opened 1 year ago

segungreat commented 1 year ago

Hello, thank you for the package, really awesome

I have been trying to add validation message, can you put me through how to add validation message in custom message and in translations.

Thank you

Melchyore commented 1 year ago

Hey @segungreat 👋

If the rules method is returning an object, you can use the messages property: https://docs.adonisjs.com/guides/validator/custom-messages.

However, if the method is returning a Validator class, you'll need to define the messages as a class attribute:

import { schema, CustomMessages } from '@ioc:Adonis/Core/Validator'
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'

export default class CreateUserValidator {
  constructor (protected ctx: HttpContextContract) {
  }

  public schema = schema.create({
  })

  public messages: CustomMessages = {}
}
segungreat commented 1 year ago

Thank you I fixed the problem

I think you need to put this in the readme file

` public rules() { return { schema: schema.create({

    email: schema.string({}, [
      rules.trim(),
      rules.email(),
      rules.disposable(),
      rules.normalizeEmail({
        allLowercase: true,
        gmailRemoveDots: true,
        gmailRemoveSubaddress: true,
      }),
      rules.escape(),
      rules.unique({ table: 'users', column: 'email', caseInsensitive: true })
    ]),
  }),
  messages: {
    required: 'The {{ field }} is required to create a new account',
    'username.unique': 'Username not available'
  }
}

}`

For ease

Melchyore commented 1 year ago

Will try to improve the docs for the next version (waiting for Adonis V6 to be released so that I can also improve this package).