Skip to content

Instantly share code, notes, and snippets.

@lbrenman lbrenman/employee.js
Last active Aug 29, 2015

Embed
What would you like to do?
Appcelerator Arrow Field Validation in the model and Multiple Field Validation in a pre Block
var Arrow = require("arrow");
var jobTitle = new Array("SA", "SE", "VP", "SAM", "SDR");
var Model = Arrow.createModel("employee",{
"fields": {
"fname": {
"type": "String"
},
"lname": {
"type": "String",
},
"tel": {
"type": "String",
// "validator": function(val) {
// if(!val.match(/(\+*\d{1,})*([ |\(])*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})/)) {
// return 'Phone Number not valid'
// }
// }
},
"title": {
"type": "String",
// "validator": function(val) {
// if (jobTitle.indexOf(val) == -1) {
// return 'Job Title not valid'
// }
// }
}
},
"before": "validateemployeeform",
"connector": "appc.arrowdb",
"actions": [
"create",
"read",
"update",
"delete",
"deleteAll"
],
"singular": "employee",
"plural": "employees"
});
module.exports = Model;
var Arrow = require('arrow');
var jobTitle = new Array("SA", "SE", "VP", "SAM", "SDR");
var PreBlock = Arrow.Block.extend({
name: 'validateemployeeform',
description: 'validate form',
action: function (req, resp, next) {
var errorArray = [];
if((req.method==="POST" || req.method==="PUT")) {
// console.log("req.params = "+JSON.stringify(req.params));
if (jobTitle.indexOf(req.params.title) == -1) {
errorArray.push('Job Title not valid');
}
if(!req.params.tel.match(/(\+*\d{1,})*([ |\(])*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})/)) {
errorArray.push('Phone Number not valid');
}
if(errorArray.length>0) {
resp.response.status(500); //workaround - https://jira.appcelerator.org/browse/API-852
resp.send({"errors": errorArray});
next(false);
} else {
next();
}
} else {
next();
}
}
});
module.exports = PreBlock;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.