adonis-form-request icon indicating copy to clipboard operation
adonis-form-request copied to clipboard

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

Open segungreat opened this issue 2 years ago • 3 comments

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

segungreat avatar Jun 03 '23 05:06 segungreat

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 = {}
}

Melchyore avatar Jun 03 '23 09:06 Melchyore

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

segungreat avatar Jun 04 '23 19:06 segungreat

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).

Melchyore avatar Jun 09 '23 12:06 Melchyore