Closed thetutlage closed 4 years ago
It will be nice if you can answer following questions.
rule <required>
or field+rule <username.required>
I want to show following error message, if email is missing
{
'email.required': 'Email address is required to create an account'
}
{
'email.required': 'Enter your account email address'
}
{
'email.required': 'Email address is required to login to your account'
}
I'm not sure if there's any convention about this, but I believe especially for those who come from laravel world find it's useful to have sort of built-in functionality
Let's just keep it simple, the default validation message should only handle common validation rules, not for specific one. If there <field>.<rule>
inside the translation file, that should be for all <field>
that have <rule>
. For example if we have email.required
that will implies to all email
field that have required
rule.
You might need to take a look this repo to see my full implementation also please take a look at the following files
For specific runtime, we should be able to override the default by usage. for example : For my repo I could simply
// SomeController.method()
const rules = {
field1: 'required|number',
field2: 'otherRules'
}
const messages = {
'field1.require': 'Y U No Fill the field'
'field1.number': 'Man, this field require a number value'
}
const data = await this.validate(request, rules, messages)
It should also support internationalization then, if there are defaults defined.
Kind of makes less sense to me, that for default messages, it will work fine and as I will try to customizes things a bit, it will just fall apart.
Also majority of apps, does have a slight variation in their messages, based upon the action. So it is simply not usable for them
hi @thetutlage . validation messages i18n and default back to the english i18n messages . and for the action veriations make the route validation class overwrite the global i18n messages . simply :
check for route validation messages
if found return them to the user
else check i18n global messages
else return english validation messages
I'm not sure I could describe it clearly but in my case
app.locales.locale
configThat's it, i guess
I have added this to the list of enhancements.
Any updates on that?
@thetutlage Hello, and thank you for providing a way to localize the custom messages.
Is there any way to achieve this using Route validators? I already created a Validator for each model so I don't use the .validate() function. Instead I use: Route.post('my-route', 'MyController.store').validator('StoreItem')
@thetutlage Hi, same question. Any updates for localized error messages from Route validators?
After exploring a lot, the following is the best I can come up with.
They are defined within the localization file as follows:
{
"required": "{field} is required"
}
{
"username.required": "{field} is required"
}
Here one can prefix the messages with an identifier and then use that identifier within the Route validator.
{
"register.username.required": "{field} is required"
}
and the validator will have an additional property
class StoreUser {
get localizationIdentifier () {
return 'register'
}
}
@thetutlage , but then how would I internationalize the field name?
I believe, you would know the field name before hand
{
"username.required": "Username is required"
}
Is this feature ready? If not, how should I localize my validation messages?
I do localization on frontend. Just use the error key received from server as an indicator for message, i18n allows you to keep all the messages in plain objects. I do it like this:
{
email: {
required: 'Email cannot be blank',
unique: 'Email should be unique'
}
}
This way you can differ messages for different situations and use any i18n library without the need to bloat validator files.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Original message: Copied from https://github.com/adonisjs/discussion/issues/62
I wonder if validator messages will looking for translation (if available) by default. For example, if we have
unique
,required
or any validations error it will looking for available translation first, otherwise use default message.The translation file would be located in
resources/locales/<locale>/validations.json
& should contains the followingI've done this by creating simple helper that dump all validation keys from
indicative
like this:That way I could simply use
validate(data, rules, Helpers.validationMessages)
, so I don't have to translate each form manually.I thought it would be great if adonis could handle this kind of functionality by default.
P.s. Hope you guys get the idea even tho my english was so poor 😁