Adding a custom function to formValidation.js configuration

136 views Asked by At

I have the following configuration which validates a form when a submit button is pressed:

 $('#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
                }
            }
        }            
    }
});

The rating field is a slider, whereby the user can choose a value between 0 and 5

enter image description here

By default it is 0, so when I press submit it validates as valid, so I need to create a custom function which will check its value if it's 0 then return invalid otherwise true.

The html is as follows:

 <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>

How the HTML looks when its render, and referenced 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">
1

There are 1 answers

6
jcuenod On BEST ANSWER

Why not try the greaterThan validator:

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