将自定义函数添加到 formValidation.js 配置

Adding a custom function to formValidation.js configuration

我有以下配置,在按下提交按钮时验证表单:

 $('#myForm').formValidation({
    framework: 'bootstrap',
    excluded: [':disabled'],
    icon: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
        title: {
            validators: {
                notEmpty: {
                    message: 'Title is required (max 128 characters)'
                }
            }
        },
        rating: {
            validators: {
                notEmpty: {
                    message: // Call function here? check for 0
                }
            }
        }            
    }
});

评级字段是一个滑块,用户可以在 0 到 5 之间选择一个值

默认情况下它是 0,所以当我按下提交时它验证为有效,所以我需要创建一个自定义函数来检查它的值,如果它是 0 那么 return 无效,否则为真。

html如下:

 <input id="Rating" name="rating" data-slider-id='ex1Slider' type="text" data-slider-min="0" data-slider-max="5" data-slider-step="1" data-slider-value="" /> <span id="rate" style="margin-left:5%">0 / 5</span>

HTML 渲染时的外观,并引用了 bootstrap-slider.js

<input id="Rating" type="text" data-slider-value="" data-slider-step="1" data-slider-max="5" data-slider-min="0" data-slider-id="ex1Slider" name="rating" data-fv-field="rating" style="display: none;" data="value: '0'" value="0">

为什么不尝试 greaterThan 验证器:

rating: {
    validators: {
        greaterThan: {
            value: 0
        }
    }
}