Last active
August 29, 2015 14:22
-
-
Save lbrenman/985261db2f9d0ce85de1 to your computer and use it in GitHub Desktop.
Appcelerator Arrow Field Validation in the model and Multiple Field Validation in a pre Block
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
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; |
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
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