66// TODO: use Yup instead of this code
77import { isUndefined , isObject , isArray , isDate , isString } from "underscore" ;
88import { fieldTypes as ft , fieldIsNumber } from "./dico" ;
9- import { locale , i18n_validation } from "../i18n/i18n" ;
9+ import { locale , i18n_validation as i18n } from "../i18n/i18n" ;
1010
1111const valRegExp = {
1212 email : / ^ [ \w . - ] + @ [ \w . - ] + \. [ \w . - ] + $ / ,
@@ -18,10 +18,13 @@ const valRegExp = {
1818
1919export const validateField = ( f , v ) => {
2020 const isNumberField = fieldIsNumber ( f ) ;
21- const formatMsg = ( fLabel , msg , r2 , r3 ) =>
22- msg . replace ( "{0}" , fLabel ) . replace ( "{1}" , r2 ) . replace ( "{2}" , r3 ) ;
23-
2421 const fieldLabel = ( f ) => f . label || f . labelShort ;
22+ const formatMsg = ( msg , r2 , r3 ) => {
23+ let vMsg = msg . replace ( "{0}" , fieldLabel ( f ) ) ;
24+ if ( r2 ) vMsg = vMsg . replace ( "{1}" , r2 ) ;
25+ if ( r3 ) vMsg = vMsg . replace ( "{2}" , r3 ) ;
26+ return vMsg ;
27+ } ;
2528
2629 if ( ! f . readOnly ) {
2730 // Check required and empty
@@ -35,7 +38,7 @@ export const validateField = (f, v) => {
3538 ( f . type === ft . list && v && ! v . length ) ) //||
3639 //(f.type===ft.color && v==='#000000')
3740 ) {
38- return formatMsg ( f . label , i18n_validation . empty ) ;
41+ return formatMsg ( i18n . empty ) ;
3942 } else if ( ! isUndefined ( v ) ) {
4043 // Check field type
4144 if ( ! ( isNaN ( v ) && isNumberField ) ) {
@@ -44,21 +47,21 @@ export const validateField = (f, v) => {
4447 case ft . int :
4548 case ft . email :
4649 if ( ! valRegExp [ f . type ] . test ( v ) ) {
47- return formatMsg ( fieldLabel ( f ) , i18n_validation [ f . type ] ) ;
50+ return formatMsg ( i18n [ f . type ] ) ;
4851 }
4952 break ;
5053 case ft . dec :
5154 case ft . money :
5255 const regex =
5356 valRegExp [ "decimal_" + locale ] || valRegExp . decimal_en ;
5457 if ( ! regex . test ( v ) ) {
55- return formatMsg ( fieldLabel ( f ) , i18n_validation [ f . type ] ) ;
58+ return formatMsg ( i18n [ f . type ] ) ;
5659 }
5760 break ;
5861 case ft . date :
5962 case ft . time :
6063 if ( v !== "" && ! isDate ( new Date ( v ) ) ) {
61- return formatMsg ( fieldLabel ( f ) , i18n_validation [ f . type ] ) ;
64+ return formatMsg ( i18n [ f . type ] ) ;
6265 }
6366 break ;
6467 case ft . json :
@@ -71,7 +74,7 @@ export const validateField = (f, v) => {
7174 } catch ( err ) { }
7275 }
7376 if ( isUndefined ( obj ) ) {
74- return formatMsg ( fieldLabel ( f ) , i18n_validation [ f . type ] ) ;
77+ return formatMsg ( i18n [ f . type ] ) ;
7578 }
7679 break ;
7780 default :
@@ -85,25 +88,21 @@ export const validateField = (f, v) => {
8588 if ( f . regExp !== null && ! isUndefined ( f . regExp ) ) {
8689 const rg = new RegExp ( f . regExp ) ;
8790 if ( ! v . match ( rg ) ) {
88- return formatMsg (
89- fieldLabel ( f ) ,
90- i18n_validation . regExp ,
91- fieldLabel ( f )
92- ) ;
91+ return formatMsg ( i18n . regExp , fieldLabel ( f ) ) ;
9392 }
9493 }
9594
9695 // Check min & max & number type
9796 if ( isNumberField ) {
9897 if ( isNaN ( v ) ) {
99- return i18n_validation . invalid ;
98+ return i18n . invalid ;
10099 }
101100 if ( v !== "" ) {
102101 if ( f . max && parseFloat ( v ) > f . max ) {
103- return formatMsg ( fieldLabel ( f ) , i18n_validation . max , f . max ) ;
102+ return formatMsg ( i18n . max , f . max ) ;
104103 }
105104 if ( f . min && parseFloat ( v ) < f . min ) {
106- return formatMsg ( fieldLabel ( f ) , i18n_validation . min , f . min ) ;
105+ return formatMsg ( i18n . min , f . min ) ;
107106 }
108107 }
109108 }
@@ -113,7 +112,7 @@ export const validateField = (f, v) => {
113112 if ( f . fnValidate ) {
114113 const fValid = f . fnValidate ( f , v ) ;
115114 if ( fValid !== "" ) {
116- return formatMsg ( fieldLabel ( f ) , fValid ) ;
115+ return formatMsg ( fValid ) ;
117116 }
118117 }
119118
@@ -124,24 +123,11 @@ export const validateField = (f, v) => {
124123 badMin = f . minLength ? len < f . minLength : false ;
125124 if ( badMax || badMin ) {
126125 if ( f . maxLength && f . minLength ) {
127- return formatMsg (
128- fieldLabel ( f ) ,
129- i18n_validation . minMaxLength ,
130- f . minLength ,
131- f . maxLength
132- ) ;
126+ return formatMsg ( i18n . minMaxLength , f . minLength , f . maxLength ) ;
133127 } else if ( f . maxLength ) {
134- return formatMsg (
135- fieldLabel ( f ) ,
136- i18n_validation . maxLength ,
137- f . maxLength
138- ) ;
128+ return formatMsg ( i18n . maxLength , f . maxLength ) ;
139129 } else {
140- return formatMsg (
141- fieldLabel ( f ) ,
142- i18n_validation . minLength ,
143- f . minLength
144- ) ;
130+ return formatMsg ( i18n . minLength , f . minLength ) ;
145131 }
146132 }
147133 }
0 commit comments