diff options
author | 2018-03-02 10:13:29 +0100 | |
---|---|---|
committer | 2018-03-02 10:13:29 +0100 | |
commit | 1d2df4a0321e6dbbef4bbfea73f0a5b67debef24 (patch) | |
tree | f6d76a54b1ea16ca5a5898d59a49cbdde0863631 /src/helpers | |
parent | fix linting errors (diff) | |
download | ferdium-app-1d2df4a0321e6dbbef4bbfea73f0a5b67debef24.tar.gz ferdium-app-1d2df4a0321e6dbbef4bbfea73f0a5b67debef24.tar.zst ferdium-app-1d2df4a0321e6dbbef4bbfea73f0a5b67debef24.zip |
internationalize validation messages
Diffstat (limited to 'src/helpers')
-rw-r--r-- | src/helpers/validation-helpers.js | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/src/helpers/validation-helpers.js b/src/helpers/validation-helpers.js index a8a242d54..2f762437d 100644 --- a/src/helpers/validation-helpers.js +++ b/src/helpers/validation-helpers.js | |||
@@ -1,6 +1,31 @@ | |||
1 | import { defineMessages } from 'react-intl'; | ||
2 | |||
3 | const messages = defineMessages({ | ||
4 | required: { | ||
5 | id: 'validation.required', | ||
6 | defaultMessage: '!!!Field is required', | ||
7 | }, | ||
8 | email: { | ||
9 | id: 'validation.email', | ||
10 | defaultMessage: '!!!Email not valid', | ||
11 | }, | ||
12 | url: { | ||
13 | id: 'validation.url', | ||
14 | defaultMessage: '!!!Not a valid URL', | ||
15 | }, | ||
16 | minLength: { | ||
17 | id: 'validation.minLength', | ||
18 | defaultMessage: '!!!Too few characters', | ||
19 | }, | ||
20 | oneRequired: { | ||
21 | id: 'validation.oneRequired', | ||
22 | defaultMessage: '!!!At least one is required', | ||
23 | }, | ||
24 | }); | ||
25 | |||
1 | export function required({ field }) { | 26 | export function required({ field }) { |
2 | const isValid = (field.value.trim() !== ''); | 27 | const isValid = (field.value.trim() !== ''); |
3 | return [isValid, `${field.label} is required`]; | 28 | return [isValid, window.franz.intl.formatMessage(messages.required, { field: field.label })]; |
4 | } | 29 | } |
5 | 30 | ||
6 | export function email({ field }) { | 31 | export function email({ field }) { |
@@ -13,7 +38,7 @@ export function email({ field }) { | |||
13 | isValid = true; | 38 | isValid = true; |
14 | } | 39 | } |
15 | 40 | ||
16 | return [isValid, `${field.label} not valid`]; | 41 | return [isValid, window.franz.intl.formatMessage(messages.email, { field: field.label })]; |
17 | } | 42 | } |
18 | 43 | ||
19 | export function url({ field }) { | 44 | export function url({ field }) { |
@@ -27,7 +52,7 @@ export function url({ field }) { | |||
27 | isValid = true; | 52 | isValid = true; |
28 | } | 53 | } |
29 | 54 | ||
30 | return [isValid, `${field.label} is not a valid url`]; | 55 | return [isValid, window.franz.intl.formatMessage(messages.url, { field: field.label })]; |
31 | } | 56 | } |
32 | 57 | ||
33 | export function minLength(length) { | 58 | export function minLength(length) { |
@@ -36,13 +61,13 @@ export function minLength(length) { | |||
36 | if (field.touched) { | 61 | if (field.touched) { |
37 | isValid = field.value.length >= length; | 62 | isValid = field.value.length >= length; |
38 | } | 63 | } |
39 | return [isValid, `${field.label} should be at least ${length} characters long.`]; | 64 | return [isValid, window.franz.intl.formatMessage(messages.minLength, { field: field.label, length })]; |
40 | }; | 65 | }; |
41 | } | 66 | } |
42 | 67 | ||
43 | export function oneRequired(targets) { | 68 | export function oneRequired(targets) { |
44 | return ({ field, form }) => { | 69 | return ({ field, form }) => { |
45 | const invalidFields = targets.filter(target => form.$(target).value === ''); | 70 | const invalidFields = targets.filter(target => form.$(target).value === ''); |
46 | return [targets.length !== invalidFields.length, `${field.label} is required`]; | 71 | return [targets.length !== invalidFields.length, window.franz.intl.formatMessage(messages.required, { field: field.label })]; |
47 | }; | 72 | }; |
48 | } | 73 | } |