Skip to content

Commit dc05f99

Browse files
Add Antl with custom locale validation messages
1 parent 969637a commit dc05f99

File tree

12 files changed

+215
-1
lines changed

12 files changed

+215
-1
lines changed

app/Validators/Event.js

+10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
const Antl = use('Antl')
2+
13
class Event {
4+
get validateAll() {
5+
return true
6+
}
7+
28
get rules() {
39
return {
410
title: 'required',
@@ -10,6 +16,10 @@ class Event {
1016
created_by: 'required|exists:users,id',
1117
}
1218
}
19+
20+
get messages() {
21+
return Antl.list('validation')
22+
}
1323
}
1424

1525
module.exports = Event

app/Validators/Forgot.js

+10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
const Antl = use('Antl')
2+
13
class Forgot {
4+
get validateAll() {
5+
return true
6+
}
7+
28
get rules() {
39
return {
410
email: 'email|required',
511
}
612
}
13+
14+
get messages() {
15+
return Antl.list('validation')
16+
}
717
}
818

919
module.exports = Forgot

app/Validators/RegisterUser.js

+10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
const Antl = use('Antl')
2+
13
class RegisterUser {
4+
get validateAll() {
5+
return true
6+
}
7+
28
get rules() {
39
return {
410
username: 'required',
@@ -7,6 +13,10 @@ class RegisterUser {
713
password: 'required',
814
}
915
}
16+
17+
get messages() {
18+
return Antl.list('validation')
19+
}
1020
}
1121

1222
module.exports = RegisterUser

app/Validators/Reset.js

+10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1+
const Antl = use('Antl')
2+
13
class Reset {
4+
get validateAll() {
5+
return true
6+
}
7+
28
get rules() {
39
return {
410
token: 'required',
511
password: 'required|confirmed',
612
}
713
}
14+
15+
get messages() {
16+
return Antl.list('validation')
17+
}
818
}
919

1020
module.exports = Reset

app/Validators/Session.js

+9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1+
const Antl = use('Antl')
12
class Session {
3+
get validateAll() {
4+
return true
5+
}
6+
27
get rules() {
38
return {
49
email: 'email|required',
510
password: 'required',
611
}
712
}
13+
14+
get messages() {
15+
return Antl.list('validation')
16+
}
817
}
918

1019
module.exports = Session

config/app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ module.exports = {
169169
| based on HTTP headers/query string.
170170
|
171171
*/
172-
locale: 'en',
172+
locale: 'pt',
173173
},
174174

175175
logger: {

database/adonis.sqlite

0 Bytes
Binary file not shown.

package-lock.json

+62
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"private": true,
1818
"dependencies": {
1919
"@adonisjs/ace": "^5.0.8",
20+
"@adonisjs/antl": "^2.0.7",
2021
"@adonisjs/auth": "^3.0.7",
2122
"@adonisjs/bodyparser": "^2.0.5",
2223
"@adonisjs/cors": "^1.0.7",

resources/locales/en/validation.json

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"above": "The {{field}} should be above {{argument.0}}.",
3+
"accepted": "The {{field}} should have been accepted",
4+
"after": "The {{field}} should be a date after {{argument.0}}",
5+
"after_offset_of": "The {{field}} should be after {{argument.0}} {{argument.1}} from today’s date",
6+
"alpha": "The {{field}} should contain letters only",
7+
"alpha_numeric": "The {{field}} should contain letters and numbers only",
8+
"array": "The {{field}} should be an ARRAY.",
9+
"before": "The {{field}} should be a date before {{argument.0}}.",
10+
"before_offset_of": "The {{field}} should be before {{argument.0}} {{argument.1}} from today’s date",
11+
"between": "The {{field}} should be between {{argument.0}} and {{argument.1}}.",
12+
"boolean": "The {{field}} should be true or false.",
13+
"confirmed": "The {{field}} confirmation does not match.",
14+
"date": "The {{field}} should be a valid date",
15+
"date_format": "The {{field}} should be a valid date according to given format {{argument.0}}.",
16+
"different": "The {{field}} and {{argument.0}} should be different.",
17+
"email": "The {{field}} should be a valid email address.",
18+
"ends_with": "The {{field}} should end with given letters ({{argument}}).",
19+
"equals": "The {{field}} should equal {{argument.0}}.",
20+
"exists": "The {{field}} should exists in table {{argument.0}}.",
21+
"file": "The {{field}} should contain a file.",
22+
"in": "The {{field}} should fall within defined values of ({{argument}}).",
23+
"includes": "The {{field}} should include define letters ({{argument}}).",
24+
"integer": "The {{field}} should be an INTEGER.",
25+
"ip": "The {{field}} should be a valid IP address.",
26+
"ipv4": "The {{field}} should be a valid IPV4 address.",
27+
"ipv6": "The {{field}} should be a valid IPV6 address.",
28+
"json": "The {{field}} should be a valid JSON string.",
29+
"max": "The {{field}} should not be more than {{argument.0}}.",
30+
"min": "The {{field}} should not be less than {{argument.0}}.",
31+
"not_contains_at_least": "The {{field}} array should contain at least {{argument.0}} item that's not {{argument.1}}.",
32+
"not_equals": "The {{field}} should be different than {{argument.0}}.",
33+
"not_in": "The {{field}} should not be one of ({{argument}}).",
34+
"object": "The {{field}} should be a valid OBJECT.",
35+
"range": "The {{field}} should be between {{argument.0}} and {{argument.1}}.",
36+
"regex": "The {{field}} format is invalid.",
37+
"required": "The {{field}} is required.",
38+
"required_if": "The {{field}} is required when {{argument.0}} exist.",
39+
"required_when": "The {{field}} is required when value of {{argument.0}} is equal to {{argument.1}}",
40+
"required_with_all": "The {{field}} is required when all of ({{argument}}) are present.",
41+
"required_with_any": "The {{field}} is required when any of ({{argument}}) are present.",
42+
"required_without_all": "The {{field}} is required when none of ({{argument}}) are present.",
43+
"required_without_any": "The {{field}} is required when any of ({{argument}}) are present.",
44+
"same": "The {{field}} and {{argument.0}} should match.",
45+
"starts_with": "The {{field}} should starts with given letters ({{argument}}).",
46+
"string": "The {{field}} should be a STRING.",
47+
"under": "The {{field}} should be under {{argument.0}}.",
48+
"unique": "The {{field}} has already been taken by someone else.",
49+
"url": "The {{field}} should be a valid url."
50+
}

resources/locales/pt/validation.json

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"above": "{{field}} deve ser maior de {{argument.0}}.",
3+
"accepted": "{{field}} deve ser aceito.",
4+
"after": "{{field}} deve ser uma data posterior à {{argument.0}}",
5+
"after_offset_of": "{{field}} deve ser posterior à {{argument.0}} {{argument.1}} da data de hoje.",
6+
"alpha": "{{field}} deve conter apenas letras.",
7+
"alpha_numeric": "{{field}} deve conter apenas letras e números.",
8+
"array": "{{field}} deve ser um array.",
9+
"before": "{{field}} deve ser uma data anterior à {{argument.0}}.",
10+
"before_offset_of": "{{field}} deve ser anterior à {{argument.0}} {{argument.1}} da data de hoje.",
11+
"between": "{{field}} deve estar entre {{argument.0}} e {{argument.1}}.",
12+
"boolean": "{{field}} deve ser true ou false.",
13+
"confirmed": "{{field}} confirmação não confere.",
14+
"date": "{{field}} deve ser uma data válida.",
15+
"date_format": "{{field}} deve ser uma data válida de acordo com o formato {{argument.0}}.",
16+
"different": "{{field}} e {{argument.0}} devem ser diferentes.",
17+
"email": "{{field}} deve ser um endereço de e-mail válido.",
18+
"ends_with": "{{field}} deve terminar com ({{argument}}).",
19+
"equals": "{{field}} deve ser igual a {{argument.0}}.",
20+
"exists": "{{field}} deve existir na tabela {{argument.0}}.",
21+
"file": "{{field}} deve conter um arquivo.",
22+
"in": "{{field}} deve ser um dos valores ({{argument}}).",
23+
"includes": "{{field}} deve incluir ({{argument}}).",
24+
"integer": "{{field}} deve ser um inteiro.",
25+
"ip": "{{field}} deve ser um endereço de IP válido.",
26+
"ipv4": "{{field}} deve ser um endereço de IPV4 válido.",
27+
"ipv6": "{{field}} deve ser um endereço de IPV6 válido.",
28+
"json": "{{field}} deve ser uma string JSON válida.",
29+
"max": "{{field}} não deve ser maior que {{argument.0}}.",
30+
"min": "{{field}} não deve ser menor que {{argument.0}}.",
31+
"not_contains_at_least": "O array {{field}} deve conter ao menos {{argument.0}} item que não seja {{argument.1}}.",
32+
"not_equals": "{{field}} deve ser diferente de {{argument.0}}.",
33+
"not_in": "{{field}} não deve ser um dos valores ({{argument}}).",
34+
"object": "{{field}} deve ser um objeto válido.",
35+
"range": "{{field}} deve estar entre {{argument.0}} e {{argument.1}}.",
36+
"regex": "O formato de {{field}} é inválido.",
37+
"required": "{{field}} é obrigatório.",
38+
"required_if": "{{field}} é obrigatório quando {{argument.0}} existir.",
39+
"required_when": "{{field}} é obrigatório quando o valor de {{argument.0}} for igual a {{argument.1}}",
40+
"required_with_all": "{{field}} é obrigatório quando ({{argument}}) estiverem presentes.",
41+
"required_with_any": "{{field}} é obrigatório quando algum de ({{argument}}) estiver presente.",
42+
"required_without_all": "{{field}} é obrigatório quando nenhum de ({{argument}}) estiver presente.",
43+
"required_without_any": "{{field}} é obrigatório quando algum de ({{argument}}) não estiver presente.",
44+
"same": "{{field}} e {{argument.0}} devem corresponder.",
45+
"starts_with": "{{field}} deve começar com ({{argument}}).",
46+
"string": "{{field}} deve ser uma string.",
47+
"under": "{{field}} deve ser menor de {{argument.0}}.",
48+
"unique": "{{field}} já está em uso.",
49+
"url": "{{field}} deve ser uma URL válida."
50+
}

start/app.js

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ const providers = [
1919
'@adonisjs/mail/providers/MailProvider',
2020
'@adonisjs/framework/providers/ViewProvider',
2121
'@adonisjs/validator/providers/ValidatorProvider',
22+
'@adonisjs/antl/providers/AntlProvider',
23+
2224
path.join(__dirname, '..', 'providers', 'CustomValidationProvider'),
2325
]
2426

0 commit comments

Comments
 (0)