After exploring this deeper, I can see that you used the Angular Datetime Picker is not fully compatible with Angular Formally .
This is because it rewrites AngularJS ngModelController.$render()
and therefore does not set the value for $touched
, like other input controls.
Another reason in your code: the configuration and the error-messages.html
template treat user control as a single element with fc.$viewValue
, fc.$viewValue
and fc.$viewValue
, while DatePicker
is rendering as a group of elements (array).
To get rid of all these problems, you can configure a custom directive to set $touched
, as shown below,
app.directive('setTouched', function MainCtrl() { return { restrict: 'A', // only activate on element attribute require: '?ngModel', // get a hold of NgModelController link: function(scope, element, attrs, ngModel) { if (!ngModel) return; // do nothing if no ng-model element.on('blur', function() { var modelControllers = scope.$eval(attrs.setTouched); if(angular.isArray(modelControllers)) { angular.forEach(modelControllers, function(modelCntrl) { modelCntrl.$setTouched(); }); } }); } }; });
And in custom-template.html
,
<div class="input-group"> <input set-touched="options.formControl" id="{{::id}}" name="{{::id}}" type="text" data-date-time-input="YYYY-MM-DD" class="form-control" data-ng-model="model['date1']"><span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span> </div>
And add fc[0].$touched
to the following configuration to take care of the array of fields,
app.run(function run(formlyConfig, formlyValidationMessages) { formlyConfig.extras.errorExistsAndShouldBeVisibleExpression = 'form.$submitted || fc.$touched || fc[0].$touched'; formlyValidationMessages.addStringMessage('required', 'This field is required'); });
And also add the section below in error-messages.html
to take care of the array of fields,
<div ng-messages="fc[0].$error" ng-if="form.$submitted || options.formControl[0].$touched" class="error-messages"> <div ng-message="{{ ::name }}" ng-repeat="(name, message) in ::options.validation.messages" class="message">{{ message(fc[0].$viewValue, fc[0].$modelValue, this)}}</div> </div>
These changes will fix the problem.
As you see some design issues with the error message that appears below,
You can modify custom-template.html
as shown below by removing the wrapper div <div class="form-group">
,
<script type="text/ng-template" id="custom-template.html"> <label class="control-label" for="{{::id}}" uib-popover="{{options.templateOptions.desc}}" popover-trigger="mouseenter" popover-placement="top-left" popover-popup-delay="500" popover-append-to-body="true">{{to.label}} {{to.required ? '*' : ''}}</label> <div class="dropdown"> <a class="dropdown-toggle" id="dropdown-{{options.key}}" role="button" data-toggle="dropdown"> <div class="input-group"> <input set-touched="options.formControl" id="{{::id}}" name="{{::id}}" type="text" data-date-time-input="YYYY-MM-DD" class="form-control" data-ng-model="model['date1']"><span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span> </div> </a> <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel"> <datetimepicker data-ng-model="model[options.key]" data-datetimepicker-config="{ dropdownSelector: '#dropdown-' + options.key, minView: 'day', startView: 'year', modelType: 'YYYY-MM-DDTHH:mm:ssZ'}"/> </ul> </div> </script>
I updated JSBin with these changes.
Excerpt
(function() { 'use strict'; var app = angular.module('formlyExample', ['formly', 'formlyBootstrap', 'ngAnimate', 'ngMessages', 'ui.bootstrap.datetimepicker', 'ui.dateTimeInput'], function config(formlyConfigProvider) { formlyConfigProvider.setType({ name: 'datepicker', templateUrl: "custom-template.html", overwriteOk: true, wrapper: ['bootstrapHasError'], defaultOptions: function defaultOptions(options) { return { templateOptions: { validation: { show: true } } }; } }); formlyConfigProvider.setWrapper({ name: 'validation', types: ['input', 'datepicker'], templateUrl: 'error-messages.html' }); }); app.run(function run(formlyConfig, formlyValidationMessages) { formlyConfig.extras.errorExistsAndShouldBeVisibleExpression = 'form.$submitted || fc.$touched || fc[0].$touched'; formlyValidationMessages.addStringMessage('required', 'This field is required'); }); app.directive('setTouched', function MainCtrl() { return { restrict: 'A',
body { margin: 20px } .formly-field { margin-bottom: 30px; } .error-messages { position: relative; } .error-messages, .message { opacity: 1; transition: .3s linear all; } .message { font-size: .8em; position: absolute; width: 100%; color: #a94442; margin-top: 4px; } .error-messages.ng-enter.ng-enter-active, .message.ng-enter.ng-enter-active { opacity: 1; top: 0; } .error-messages.ng-enter, .message.ng-enter { opacity: 0; top: -10px; } .error-messages.ng-leave, .message.ng-leave { opacity: 1; top: 0; } .error-messages.ng-leave-active, .message.ng-leave-active { opacity: 0; top: -10px; }
<!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.css" rel="stylesheet"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="//npmcdn.com/ api-check@latest /dist/api-check.js"></script> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script> <script src="//npmcdn.com/ angular-formly@latest /dist/formly.js"></script> <script src="//npmcdn.com/ angular-formly-templates-bootstrap@latest /dist/angular-formly-templates-bootstrap.js"></script> <script src="https://rawgit.com/angular/bower-angular-messages/v1.4.4/angular-messages.js"></script> <script src="https://rawgit.com/angular/bower-angular-animate/v1.4.4/angular-animate.js"></script> <script src="https://cdn.rawgit.com/moment/moment/develop/min/moment-with-locales.min.js"></script> <script type="text/javascript" src="https://cdn.rawgit.com/dalelotts/angular-bootstrap-datetimepicker/master/src/js/datetimepicker.js"></script> <script type="text/javascript" src="https://cdn.rawgit.com/dalelotts/angular-bootstrap-datetimepicker/master/src/js/datetimepicker.templates.js"></script> <link href="https://cdn.rawgit.com/dalelotts/angular-bootstrap-datetimepicker/master/src/css/datetimepicker.css" rel="stylesheet"> <script type="text/javascript" src="https://cdn.rawgit.com/dalelotts/angular-date-time-input/master/src/dateTimeInput.js"></script> <title>Angular Formly Example</title> </head> <body ng-app="formlyExample" ng-controller="MainCtrl as vm"> <div> <form ng-submit="vm.onSubmit()" name="vm.form" novalidate> <formly-form model="vm.model" fields="vm.fields" options="vm.options" form="vm.form"> <button type="submit" class="btn btn-primary submit-button">Submit</button> <button type="button" class="btn btn-default" ng-click="vm.options.resetModel()">Reset</button> </formly-form> </form> <hr /> <h2>Model</h2> <pre>{{vm.model | json}}</pre> <h2>Fields <small>(note, functions are not shown)</small></h2> <pre>{{vm.originalFields | json}}</pre> <h2>Form</h2> <pre>{{vm.form | json}}</pre> </div> <script type="text/ng-template" id="custom-template.html"> <label class="control-label" for="{{::id}}" uib-popover="{{options.templateOptions.desc}}" popover-trigger="mouseenter" popover-placement="top-left" popover-popup-delay="500" popover-append-to-body="true">{{to.label}} {{to.required ? '*' : ''}}</label> <div class="dropdown"> <a class="dropdown-toggle" id="dropdown-{{options.key}}" role="button" data-toggle="dropdown"> <div class="input-group"> <input set-touched="options.formControl" id="{{::id}}" name="{{::id}}" type="text" data-date-time-input="YYYY-MM-DD" class="form-control" data-ng-model="model['date1']"><span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span> </div> </a> <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel"> <datetimepicker data-ng-model="model[options.key]" data-datetimepicker-config="{ dropdownSelector: '#dropdown-' + options.key, minView: 'day', startView: 'year', modelType: 'YYYY-MM-DDTHH:mm:ssZ'}"/> </ul> </div> </script> <script type="text/ng-template" id="error-messages.html"> <formly-transclude></formly-transclude> <div ng-messages="fc.$error" ng-if="form.$submitted || options.formControl.$touched" class="error-messages"> <div ng-message="{{ ::name }}" ng-repeat="(name, message) in ::options.validation.messages" class="message">{{ message(fc.$viewValue, fc.$modelValue, this)}}</div> </div> <div ng-messages="fc[0].$error" ng-if="form.$submitted || options.formControl[0].$touched" class="error-messages"> <div ng-message="{{ ::name }}" ng-repeat="(name, message) in ::options.validation.messages" class="message">{{ message(fc[0].$viewValue, fc[0].$modelValue, this)}}</div> </div> </script> </body> </html>