Skip to content

Commit 733f6d2

Browse files
authored
1.2.0 (dockwa#102) Localization and Typescript
* first stab at multilang support in seperate files * folder typo * replace en with fr since en is default * correct module return * readme * will be on master * spanish support and template example * camelCase return name * /min directory in /locale * version bump * example to add later * add link to message option * words * template * feat(ts definition): init * update index.d.ts
1 parent 21c4cda commit 733f6d2

21 files changed

+531
-20
lines changed

License

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License
22

3-
Copyright (c) 2018 Dockwa.
3+
Copyright (c) 2019 Dockwa.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ Simple React Validator is exactly as it sounds. We wanted to build a validator f
2424
2. [Setup](#3-easy-steps)
2525
3. [Rules](#rules)
2626
4. [Options](#options)
27+
1. [Element](#1-element)
28+
2. [Class Name](#2-classname)
29+
3. [Messages](#3-messages)
30+
4. [Validators](#4-validators)
31+
5. [Auto Force Update](#5-autoforceupdate)
32+
6. [Localization](#6-locale)
2733
5. [Custom Validators](#custom-validators)
2834

2935
# Usage
@@ -352,7 +358,8 @@ Must be a valid url. Ex. https://dockwa.com or dockwa.com
352358

353359
# Options
354360
The Simple React Validator can receive an options object when initialized or as the fourth parameter when defining a validator. There are 4 options you can provide.
355-
1. element: Accepts a block where you can return the default element that you want to wrap the message from a validator message. The default element is `<div className="srv-validation-message">{message}</div>`. If you are using React Native the default will be just the message the gets returned. You can also set `element: false` to just return a message.
361+
#### 1. element:
362+
Accepts a block where you can return the default element that you want to wrap the message from a validator message. The default element is `<div className="srv-validation-message">{message}</div>`. If you are using React Native the default will be just the message the gets returned. You can also set `element: false` to just return a message.
356363
* **Takes 2 params**
357364
* message: The message coming from the validator.
358365
* className (optional): Will optionally be provided so you can change the className on a per validation basis.
@@ -361,21 +368,40 @@ this.validator = new SimpleReactValidator({
361368
element: message => <div>{message}</div>
362369
// OR
363370
element: (message, className) => <div className={className}>{message}</div>
364-
}
371+
})
365372
```
366-
2. className: String of classes to be passed into an element, default is `srv-validation-message` and can be overriden.
367-
3. messages: Accepts an object to override validation messages. It also accepts a default which will override all messages.
373+
#### 2. className:
374+
String of classes to be passed into an element, default is `srv-validation-message` and can be overriden.
375+
#### 3. messages:
376+
Accepts an object to override validation messages. It also accepts a default which will override all messages.
368377
```jsx
369378
this.validator = new SimpleReactValidator({
370379
messages: {
371380
email: 'That is not an email.'
372381
// OR
373382
default: 'Validation has failed!' // will override all messages
374383
},
375-
}
384+
})
385+
```
386+
#### 4. validators:
387+
Accepts an object of custom validators. See [Custom Validators](#customvalidators) for more info on defining custom validators.
388+
#### 5. autoForceUpdate:
389+
Accepts a react instance and will automatically be called when messages are shown and hidden automatically. [More on autoForceUpdate](#note-autoforceupdate)
390+
#### 6. locale:
391+
Accepts a string with the localized messages of your choice. **For this to work, the correct language file also needs to be loaded into your front end.** [Current Supported Languages](https://github.com/dockwa/simple-react-validator/tree/master/src/locale). To contribute to translating the project use [this file as a template.](https://github.com/dockwa/simple-react-validator/blob/master/src/locale/template-en.js)
392+
```jsx
393+
// sets french default validation messages.
394+
this.validator = new SimpleReactValidator({locale: 'fr'});
395+
```
396+
You can apply custom messages with the [messages](#3-messages) option. However you can also apply a custom language that you can later select with the `addLocale` class method.
397+
```jsx
398+
SimpleReactValidator.addLocale('klingon', {
399+
accepted: 'Hab SoSlI’ Quch!',
400+
email: 'Heghlu’meH QaQ jajvam'
401+
});
402+
...
403+
this.validator = new SimpleReactValidator({locale: 'klingon'});
376404
```
377-
4. validators: Accepts an object of custom validators. See [Custom Validators](#customvalidators) for more info on defining custom validators.
378-
5. autoForceUpdate: Accepts a react instance and will automatically be called when messages are shown and hidden automatically. [More on autoForceUpdate](#note-autoforceupdate)
379405

380406

381407
# Custom Validators

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "simple-react-validator",
33
"description": "A simple react form validator inspired by Laravel validation.",
44
"main": "dist/simple-react-validator.min.js",
5-
"version": "1.1.0",
5+
"version": "1.2.0",
66
"authors": [
77
"Stuart Yamartino"
88
],

dist/locale/es.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Simple React Validator v1.2.0 | Created By Dockwa | MIT License | 2017 - Present
2+
;(function(root, factory) {
3+
if (typeof define === 'function' && define.amd) {
4+
define(['simple-react-validator'], factory);
5+
} else if (typeof exports === 'object') {
6+
module.exports = factory(require('simple-react-validator'));
7+
} else {
8+
root.SimpleReactValidatorLocaleEs = factory(root.SimpleReactValidator);
9+
}
10+
}(this, function(SimpleReactValidator) {
11+
"use strict";
12+
13+
var es = {
14+
accepted: ':attribute debe ser aceptado.',
15+
after: ':attribute debe ser una fecha posterior a :date.',
16+
after_or_equal: ':attribute debe ser una fecha posterior o igual a :date.',
17+
alpha: ':attribute sólo debe contener letras.',
18+
// alpha_space : 'The :attribute may only contain letters and spaces.',
19+
// alpha_num : 'The :attribute may only contain letters and numbers.',
20+
// alpha_num_space : 'The :attribute may only contain letters, numbers, and spaces.',
21+
// alpha_num_dash : 'The :attribute may only contain letters, numbers, and dashes.',
22+
// alpha_num_dash_space : 'The :attribute may only contain letters, numbers, dashes, and spaces.',
23+
array: ':attribute debe ser un conjunto.',
24+
before: ':attribute debe ser una fecha anterior a :date.',
25+
before_or_equal: ':attribute debe ser una fecha anterior o igual a :date.',
26+
between: ':attribute tiene que estar entre :min - :max:type.',
27+
"boolean": 'El campo :attribute debe tener un valor verdadero o falso.',
28+
// card_exp : 'The :attribute must be a valid expiration date.',
29+
// card_num : 'The :attribute must be a valid credit card number.',
30+
// currency : 'The :attribute must be a valid currency.',
31+
date: ':attribute no es una fecha válida.',
32+
date_equals: ':attribute debe ser una fecha igual a :date.',
33+
email: ':attribute no es un correo válido',
34+
"in": ':attribute es inválido :values.',
35+
integer: ':attribute debe ser un número entero.',
36+
max: ':attribute no debe ser mayor a :max:type.',
37+
min: 'El tamaño de :attribute debe ser de al menos :min:type.',
38+
not_in: ':attribute es inválido :values.',
39+
not_regex: 'El formato del campo :attribute no es válido.',
40+
numeric: ':attribute debe ser numérico.',
41+
// phone : 'The :attribute must be a valid phone number.',
42+
regex: 'El formato de :attribute es inválido.',
43+
required: 'El campo :attribute es obligatorio.',
44+
size: 'El tamaño de :attribute debe ser :size:type.',
45+
string: 'El campo :attribute debe ser una cadena de caracteres.',
46+
// typeof : 'The :attribute is not the correct type of :type.',
47+
url: 'El formato :attribute es inválido.'
48+
};
49+
SimpleReactValidator.addLocale('es', es);
50+
return es;
51+
}));

dist/locale/fr.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Simple React Validator v1.2.0 | Created By Dockwa | MIT License | 2017 - Present
2+
;(function(root, factory) {
3+
if (typeof define === 'function' && define.amd) {
4+
define(['simple-react-validator'], factory);
5+
} else if (typeof exports === 'object') {
6+
module.exports = factory(require('simple-react-validator'));
7+
} else {
8+
root.SimpleReactValidatorLocaleFr = factory(root.SimpleReactValidator);
9+
}
10+
}(this, function(SimpleReactValidator) {
11+
"use strict";
12+
13+
var fr = {
14+
accepted: 'Le champ :attribute doit être accepté.',
15+
after: 'Le champ :attribute doit être après :date.',
16+
after_or_equal: 'Le champ :attribute doit correspondre ou bien être après :date.',
17+
alpha: 'Le champ :attribute ne peut contenir que des lettres.',
18+
alpha_space: 'Le champ :attribute ne peut contenir que des lettres et des espaces.',
19+
alpha_num: 'Le champ :attribute ne peut contenir que des lettres et des chiffres.',
20+
alpha_num_space: 'Le champ :attribute ne peut contenir que des lettres, chiffres, et espaces.',
21+
alpha_num_dash: 'Le champ :attribute ne peut contenir que des lettres, chiffres, et tirets.',
22+
alpha_num_dash_space: 'Le champ :attribute ne peut contenir que des lettres, chiffres, tirets, et espaces.',
23+
array: 'Le champ :attribute doit êttre un tableau.',
24+
before: 'Le champ :attribute doit être avant :date.',
25+
before_or_equal: 'Le champ :attribute doit correspondre ou bien être avant :date.',
26+
between: 'Le champ :attribute doit être entre :min et :max:type.',
27+
"boolean": 'Le champ :attribute doit être booléen.',
28+
card_exp: "Le champ :attribute doit être une date d'expiration valide.",
29+
card_num: 'Le champ :attribute doit être un numéro valide de carte de crédit .',
30+
currency: 'Le champ :attribute doit être une devise valide.',
31+
date: 'Le champ :attribute doit être une date.',
32+
date_equals: 'Le champ :attribute doit correspondre à :date.',
33+
email: 'Le champ :attribute doit êre une adresse email valide.',
34+
"in": 'Le champ selectionné :attribute doit être :values.',
35+
integer: 'Le champ :attribute doit être un entier.',
36+
max: 'Le champ :attribute ne doit pas dépasser :max:type.',
37+
min: 'Le champ :attribute doit au moins être :min:type.',
38+
not_in: 'Le champ selectionné :attribute ne doit pas être :values.',
39+
not_regex: 'Le champ :attribute ne doit pas correspondre au motif requis.',
40+
numeric: 'Le champ :attribute doit être un chiffre.',
41+
phone: 'Le champ :attribute doit être un numéro de téléphone valide.',
42+
regex: 'Le champ :attribute doit correspondre au motif requis.',
43+
required: 'Le champ :attribute est requis.',
44+
size: 'Le champ :attribute doit être :size:type.',
45+
string: 'Le champ :attribute doit être une chaîne.',
46+
"typeof": "Le champ :attribute n'est pas le type correcte de :type.",
47+
url: 'Le champ :attribute doit être un url.'
48+
};
49+
SimpleReactValidator.addLocale('fr', fr);
50+
return fr;
51+
}));

dist/locale/min/es.min.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/locale/min/fr.min.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/locale/min/template-en.min.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/locale/template-en.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Simple React Validator v1.2.0 | Created By Dockwa | MIT License | 2017 - Present
2+
;(function(root, factory) {
3+
if (typeof define === 'function' && define.amd) {
4+
define(['simple-react-validator'], factory);
5+
} else if (typeof exports === 'object') {
6+
module.exports = factory(require('simple-react-validator'));
7+
} else {
8+
root.SimpleReactValidatorLocaleTemplateEn = factory(root.SimpleReactValidator);
9+
}
10+
}(this, function(SimpleReactValidator) {
11+
"use strict";
12+
13+
/******* This in an example template ***********
14+
This file does nothing since English is the default language
15+
build into simple react validator.
16+
17+
Copy this file to add more
18+
supported languages. This is a great reference for translations
19+
of these messages: https://github.com/caouecs/Laravel-lang/tree/master/src
20+
21+
If you don't have a translation for a line, comment out that line
22+
and move on. It is smart enough to fall back to the default English
23+
and hopefully someone else will contribute to fill in the gaps later!
24+
25+
Note: the "const" variable name will be the camelCase filename.
26+
/**********************************************/
27+
var templateEn = {
28+
accepted: 'The :attribute must be accepted.',
29+
after: 'The :attribute must be after :date.',
30+
after_or_equal: 'The :attribute must be after or on :date.',
31+
alpha: 'The :attribute may only contain letters.',
32+
alpha_space: 'The :attribute may only contain letters and spaces.',
33+
alpha_num: 'The :attribute may only contain letters and numbers.',
34+
alpha_num_space: 'The :attribute may only contain letters, numbers, and spaces.',
35+
alpha_num_dash: 'The :attribute may only contain letters, numbers, and dashes.',
36+
alpha_num_dash_space: 'The :attribute may only contain letters, numbers, dashes, and spaces.',
37+
array: 'The :attribute must be an array.',
38+
before: 'The :attribute must be before :date.',
39+
before_or_equal: 'The :attribute must be before or on :date.',
40+
between: 'The :attribute must be between :min and :max:type.',
41+
"boolean": 'The :attribute must be a boolean.',
42+
card_exp: 'The :attribute must be a valid expiration date.',
43+
card_num: 'The :attribute must be a valid credit card number.',
44+
currency: 'The :attribute must be a valid currency.',
45+
date: 'The :attribute must be a date.',
46+
date_equals: 'The :attribute must be on :date.',
47+
email: 'The :attribute must be a valid email address.',
48+
"in": 'The selected :attribute must be :values.',
49+
integer: 'The :attribute must be an integer.',
50+
max: 'The :attribute may not be greater than :max:type.',
51+
min: 'The :attribute must be at least :min:type.',
52+
not_in: 'The selected :attribute must not be :values.',
53+
not_regex: 'The :attribute must not match the required pattern.',
54+
numeric: 'The :attribute must be a number.',
55+
phone: 'The :attribute must be a valid phone number.',
56+
regex: 'The :attribute must match the required pattern.',
57+
required: 'The :attribute field is required.',
58+
size: 'The :attribute must be :size:type.',
59+
string: 'The :attribute must be a string.',
60+
"typeof": 'The :attribute is not the correct type of :type.',
61+
url: 'The :attribute must be a url.'
62+
};
63+
SimpleReactValidator.addLocale('en', templateEn);
64+
return templateEn;
65+
}));

0 commit comments

Comments
 (0)