aw-front-helpers 2.5.23 → 2.5.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,6 +27,12 @@ const defaultOptions = {
27
27
  radio: window.Translator
28
28
  ? window.Translator.trans('form_errors.radio')
29
29
  : 'Veuillez sélectionner au moins un choix.',
30
+ minLength: window.Translator
31
+ ? window.Translator.trans('form_errors.min_length')
32
+ : 'Ce champ doit contenir au minimum %length% caractères',
33
+ maxLength: window.Translator
34
+ ? window.Translator.trans('form_errors.max_length')
35
+ : 'Ce champ doit contenir au maximum %length% caractères',
30
36
  pattern: window.Translator
31
37
  ? window.Translator.trans('form_errors.pattern')
32
38
  : 'La donnée saisie ne respecte pas les règles énoncées.',
@@ -112,6 +118,25 @@ export default class FormControl {
112
118
  this._formGrabDom();
113
119
  }
114
120
 
121
+ addControl(el, controlName, test, errorMessage) {
122
+ if(!el) return
123
+ el.specialControls = el.specialControls || {};
124
+ el.specialControls[controlName] = {
125
+ isValid: () => test(el.value), errorMessage
126
+ }
127
+ }
128
+
129
+ checkError(el) {
130
+ this._checkError(el);
131
+ }
132
+
133
+ removeControl(el, controlName) {
134
+ if(!el) return
135
+ if(el.specialControls && el.specialControls[controlName]) {
136
+ delete el.specialControls[controlName];
137
+ }
138
+ }
139
+
115
140
  removeErrors(el = null) {
116
141
  this._removeError(el);
117
142
  }
@@ -248,7 +273,24 @@ export default class FormControl {
248
273
  invalid = true;
249
274
  this.setError(el, this._options.i18n.same);
250
275
  }
251
-
276
+ if (!testMinLength(el, this._form)) {
277
+ invalid = true;
278
+ const minLength = el.getAttribute('minlength');
279
+ this.setError(el, this._options.i18n.minLength.replace('%length%', minLength));
280
+ }
281
+ if (!testMaxLength(el, this._form)) {
282
+ invalid = true;
283
+ const maxLength = el.getAttribute('maxlength');
284
+ this.setError(el, this._options.i18n.maxLength.replace('%length%', maxLength));
285
+ }
286
+ if( el.specialControls ) {
287
+ for( let key in el.specialControls ) {
288
+ if( !el.specialControls[key].isValid() ) {
289
+ invalid = true;
290
+ this.setError(el, el.specialControls[key].errorMessage);
291
+ }
292
+ }
293
+ }
252
294
  return invalid;
253
295
  }
254
296
 
@@ -335,6 +377,32 @@ export function testRequired(el, form) {
335
377
  return el.value !== '';
336
378
  }
337
379
 
380
+ /**
381
+ * Test required input minlength is valid
382
+ * @param {Element} el Input to test
383
+ * @param {Element} form Formulaire
384
+ * @return {Boolean} Returns if element is valid or not
385
+ */
386
+ export function testMinLength(el, form) {
387
+ const minLength = el.getAttribute('minlength');
388
+ if (minLength === null) return true;
389
+ const value = el.value.trim();
390
+ return value === '' || (value !== '' && value.length >= parseInt(minLength))
391
+ }
392
+
393
+ /**
394
+ * Test required input maxlength is valid
395
+ * @param {Element} el Input to test
396
+ * @param {Element} form Formulaire
397
+ * @return {Boolean} Returns if element is valid or not
398
+ */
399
+ export function testMaxLength(el, form) {
400
+ const maxLength = el.getAttribute('maxlength');
401
+ if (maxLength === null) return true;
402
+ const value = el.value.trim();
403
+ return value === '' || (value !== '' && value.length <= parseInt(maxLength))
404
+ }
405
+
338
406
  /**
339
407
  * Test input of email type is valid
340
408
  * @param {Element} el Input to test
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aw-front-helpers",
3
- "version": "2.5.23",
3
+ "version": "2.5.25",
4
4
  "description": "Assets helpers for projects",
5
5
  "author": "Yoann Le Poupon",
6
6
  "license": "ISC",