rivets-backbone-is-required.js 902 Bytes
define(function(require) {
    'use strict';
    var _ = require('underscore');
    var rivets = require('rivets');

    rivets.formatters['is-required'] = function(obj, fieldName) {
        if (!obj) {
            return false;
        }
        var validation = obj.validation;
        if (!validation) {
            return false;
        }
        var validators = validation[fieldName];
        if (!validators) {
            return false;
        }
        if(!_.isArray(validators)) {
            validators = [validators];
        }
        var computed = obj.toJSON();
        return _.filter(validators, function(validator) {
            var required = validator.required;
            if (_.isFunction(required)) {
                return required.call(this, null, fieldName, computed);
            } else {
                return required;
            }
        }, obj).length > 0;
    };
});