How to use custom validators of github.com/1000hz/bootstrap-validator

前端 未结 3 507
后悔当初
后悔当初 2020-12-19 02:53

From the documentation http://1000hz.github.io/bootstrap-validator/:

Add custom validators to be run. Validators should be functions that receive the

3条回答
  •  不知归路
    2020-12-19 03:19

    First of all add your own custom validator, for example:

    var validatorOptions = {
            delay: 100,
            custom: {
                unique: function ($el) {
                    var newDevice = $el.val().trim();
                    return isUniqueDevice(newDevice)
                }
            },
            errors: {
                unique: "This Device is already exist"
            }
        }
    

    Second, you need to "bind" the form input for the custom validator, for example:

    
    

    If you want to add to this input more validators error you must to add the custom error to the input: data-unique-error="This device is already exist" for example:

    
    

    The "data-error" is the default validator error and it called "native" key, the following code will demonstrate how the validator print the error messages according to the validator key:

       function getErrorMessage(key) {
      return $el.data(key + '-error')
        || $el.data('error')
        || key == 'native' && $el[0].validationMessage
        || options.errors[key]
    }
    

提交回复
热议问题