internationalization validation fields #3590
Unanswered
farshadfahimi
asked this question in
Help
Replies: 2 comments
-
Hello @farshadfahimi I have the same problem ! |
Beta Was this translation helpful? Give feedback.
0 replies
-
I solved it this way. import { schema, rules, CustomMessages } from '@ioc:Adonis/Core/Validator'
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
export default class StoreUserValidator {
constructor(protected ctx: HttpContextContract) {}
public schema = schema.create({
email: schema.string([
rules.required(),
rules.email(),
rules.unique({ table: 'users', column: 'email' })
]),
password: schema.string([rules.required()]),
})
// Internationalized fields
get fields() {
return {
email: this.ctx.i18n.formatMessage('user.email'),
password: this.ctx.i18n.formatMessage('user.password'),
}
}
// Custom Validator Messages
public messages: CustomMessages = {
'*': (field, rule, arrayExpressionPointer, options) => {
try {
return this.ctx.i18n.formatMessage(`validator.shared.${rule}`, { field: this.fields[field] })
} catch (_) {
return this.ctx.i18n.validatorMessages()['*'](field, rule, arrayExpressionPointer, options)
}
},
}
} const payload = await request.validate(StoreUserValidator) # validator.yaml
shared:
required: "{field} is required."
email: "{field} is not correct."
unique: "{field} is already exist."
# user.yaml
email: "Email"
password: "Password" |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have a
validator.json
file like belowin my route validate the request like below
the response content message is
enter name is required
but i need to rename the
field
property in the message and the message content should beenter username is required"
.ofcourse I could have json style like below
this way is easy for small translation. but I have lot's of field and just need to rename the field and save other content
Do you have any idea about this?
Beta Was this translation helpful? Give feedback.
All reactions