carl carl - 4 months ago 15
jQuery Question

formValidation with datetimepicker and format('LLLL')

I am displaying the time in the datetimepicker in the format LLLL by using the following options

$('#meeting_datetime').datetimepicker({defaultDate: tomorrowsDate, stepping: 5, format: 'LLLL'});


all that works fine, but now I would like to validate that field and I thought it should just work as

$('#manage_jc_settings_form')
.formValidation({
framework: 'bootstrap',
excluded: ':disabled',
ignore: [],
icon: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
meeting_datetime: {
validators: {
notEmpty: {
message: 'The date is required'
},
date: {
format: 'LLLL',
message: 'The date is not valid'
},
callback: {
message: "The next meeting can't be in the past",
callback: function (value, validator) {
var m = new moment(value, 'LLLL', true);
return (m > todaysDate);
}
}
}
}
}
}
});


but this way the datetime field does not validate. Does anybody know how to use the LLLL format in the validation? I am using the formValidation plugin http://formvalidation.io/
thanks
carl

Answer

The date validation type does not support that type of formatting. You need to use the callback validation type with moment. However, since you already have a callback type and need to return a different message, you need to use the Dynamic message.

http://formvalidation.io/validators/callback/#dynamic-message-example

function(value, validator, $field) {
    // ... Do your logic checking
    if (...) {
        return {
            valid: true,    // or false
            message: 'The error message'
        }
    }

    return {
        valid: false,       // or true
        message: 'Other error message'
    }
}

Then do a check for the validity of the date and return one message for that. And check the date is greater than the todaysDate and return a different message for that.