Search code examples
angularjsvalidationangular-ui

manually set validity in bootstrap validator


Is is possible to manually set field validity to invalid in bootstrap validator?

I'm using angular js in my project and want to use date range validation.

The "max-date" attribute works fine except if I'm typing a date manually, that's why I want revalidate field in the controller and manually set field validity.

html:

    <div class="form-group inputGroupContainer" ng-controller="DatepickerCtrl">
        <label for="date">Date of Birth</label>
        <p class="input-group">
            <input type="text" name="date1"  placeholder="DD/MM/YYYY" class="form-control"  ui-date="{dateFormat: 'dd/MM/yyyy'}" ui-date-format="dd/MM/yyyy" datepicker-popup="{{format}}" ng-model="user.Personal.BirthDate" max-date="currentDate" is-open="opened" close-text="Close" />
            <span class="input-group-btn">
                <button type="button"   class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
            </span>
        </p>
    </div>

angularjs controller:

$scope.$watch('user.Personal.BirthDate', function (newValue, oldValue) {
                    if (newValue !== oldValue) {
                        if ($scope.user.Personal.BirthDate)
                            if ($scope.user.Personal.BirthDate.getTime() > new Date().getTime()) {
//set validity for "date1" to invalid
                            }
                        $($('#editPersonalForm')).bootstrapValidator('revalidateField', 'date1');
                    }
                });

Solution

  • This plugin has recently changed names formValidation and licensing but back at 0.5.0 this worked for me.

    Force the status of an input to INVALID

    /**
         * Update all validating results of field
         *
         * @param {String|jQuery} field The field name or field element
         * @param {String} status The status. Can be 'NOT_VALIDATED', 'VALIDATING', 'INVALID' or 'VALID'
         * @param {String} [validatorName] The validator name. If null, the method updates validity result for all validators
         * @returns {BootstrapValidator}
         */
    $('#editPersonalForm').data('bootstrapValidator').updateStatus('date1', 'INVALID', null)