Skip to content

Instantly share code, notes, and snippets.

@philayres
Created May 1, 2014 17:57
Show Gist options
  • Save philayres/653be9b1c61d041c9b78 to your computer and use it in GitHub Desktop.
Save philayres/653be9b1c61d041c9b78 to your computer and use it in GitHub Desktop.
Hack Alpaca checkbox fields to validate against JSON Schema
/* Include the following functions to override those in the appropriate places in the Alpaca.js code */
//Alpaca.ControlField = {
validationList: function(){
var list = this.schema.enum;
if(!list) list = this.schema.list;
return list;
},
handleValidate: function() {
var baseStatus = this.base();
var valInfo = this.validation;
var status = this._validateEnum();
var list = this.validationList();
valInfo["invalidValueOfEnum"] = {
"message": status ? "" : Alpaca.substituteTokens(this.view.getMessage("invalidValueOfEnum"), [list.join(',')]),
"status": status
}
return baseStatus && valInfo["invalidValueOfEnum"]["status"];
};
//}
//Alpaca.Fields.CheckBoxField = {
getEnum: function()
{
var array = [];
var list = this.validationList();
if (list)
{
array = list;
}
return array;
},
_validateEnum: function()
{
var self = this;
if (!self.options.multiple)
{
return true;
}
var val = self.getValue();
if (!self.schema.required && Alpaca.isValEmpty(val))
{
return true;
}
// if val is a string, convert to array
if (typeof(val) == "string")
{
val = val.split(",");
}
var list = this.validationList();
return Alpaca.anyEquality(val, list);
}
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment