Created
January 28, 2015 21:36
-
-
Save MaxMorais/6e0f3153663f30baf883 to your computer and use it in GitHub Desktop.
Custom Formatters for frappe ControlData
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
frappe.ui.form.ControlData = frappe.ui.form.ControlData.extend({ | |
format_for_input: function(v, callback){ | |
v = this._super(v, callback); | |
if (v+''==='') return ''; | |
switch (this.df.options){ | |
case('Phone'): | |
if (v+''=='') return ''; | |
// phone may start with + and must only have numbers later, '-' and ' ' are stripped | |
v = v.replace(/ /g, '').replace(/-/g, '').replace(/\(/g, '').replace(/\)/g, ''); | |
// allow initial +,0,00 | |
if(v && v.substr(0,1)=='+') { | |
v1 = '+'; v = v.substr(1); | |
} | |
if(v && v.substr(0,2)=='00') { | |
v1 += '00'; v = v.substr(2); | |
} | |
if(v && v.substr(0,1)=='0') { | |
v1 += '0'; v = v.substr(1); | |
} | |
v1 += cint(v) + ''; | |
callback(v1); | |
break; | |
case ('Email'): | |
if(!validate_email(v)) { | |
msgprint(format(__("Invalid Email: {0}", [v]))); | |
callback(""); | |
} else { | |
callback(v); | |
} | |
break; | |
case ('CPF/CNPJ'): | |
var v1 = v.replace(/[^0-9]/, '', v); | |
if (!eCpfCnpj(v1)){ | |
msgprint(format(__("Invalid {0}: {1}", [ | |
(v1=="") ? "CPF/CNPJ" : v1.length <= 11? "CPF": "CNPJ", | |
v1]))) | |
if (typeof(callback)==='function') { | |
return callback(""); | |
} | |
return ''; | |
} else { | |
if (v1.length===11){ | |
v1 = format("{0}.{1}.{2}-{3}", [ | |
v1.slice(0,3), | |
v1.slice(3,6), | |
v1.slice(6,9), | |
v1.slice(9,11) | |
]); | |
} else { | |
v1 = format("{0}.{1}.{2}/{3}-{4}", [ | |
v1.slice(0,2), | |
v1.slice(2,5), | |
v1.slice(5,8), | |
v1.slice(8,12), | |
v1.slice(12,14) | |
]); | |
} | |
if (typeof(callback)==='function') { | |
return callback(v1); | |
} | |
return v1; | |
} | |
break; | |
case ('CEP'): | |
if (/^[0-9]{5}-?[0-9]{3}$/.test(v)){ | |
if (typeof(callback)==='function') { | |
return callback(v); | |
} | |
return v; | |
} else { | |
msgprint(format(__("Invalid {0}: {0}", ["CEP", v]))); | |
if (typeof(callback)==='function') { | |
return callback(""); | |
} | |
return "" | |
} | |
default: | |
if (typeof(callback)=== 'function') {return callback(v);} | |
return v; | |
break; | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment