Validation Error Message Does Not Appear for DatePicker Angular Formly Custom Field

I am trying to use Angular Datetime Picker as Angular Formally . I have such work that I can edit and set a value that is correctly added to the bound model.

However, I cannot receive validation error messages so that they appear as in regular input fields.

JS Bin with what I still have . As you can see, the red color does not appear when you leave the field, only when you are trying to send. And the error message never appears.

Formal configuration :

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

Fields

 vm.fields = [ { key: 'text', type: 'input', templateOptions: { label: 'Text', placeholder: 'Write something', required: true }, }, { key: 'date', type: 'datepicker', templateOptions: { label: 'Date', placeholder: 'Pick a date', required: true }, } ]; 

Patterns

 <script type="text/ng-template" id="custom-template.html"> <div class="form-group"> <label class="control-label" for="{{::id}}">{{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 id="{{::id}}" name="{{::id}}" type="text" data-date-time-input="YYYY-MM-DD" class="form-control" data-ng-model="model[options.key]"><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> </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> </script> 
+6
source share
2 answers

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

 /* global angular */ (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', // 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(); }); } }); } }; }); app.controller('MainCtrl', function MainCtrl(formlyVersion) { var vm = this; vm.onSubmit = onSubmit; vm.model = {}; vm.options = {}; vm.env = { angularVersion: angular.version.full, formlyVersion: formlyVersion }; vm.fields = [ { key: 'text', type: 'input', templateOptions: { label: 'Text', placeholder: 'Write something', required: true }, }, { key: 'moretext', type: 'input', templateOptions: { label: 'More Text', placeholder: 'Write something else', }, }, { key: 'date', type: 'datepicker', templateOptions: { label: 'Date', placeholder: 'Pick a date', required: true }, } ]; vm.originalFields = angular.copy(vm.fields); // function definition function onSubmit() { if (vm.form.$valid) { vm.options.updateInitialValue(); alert(JSON.stringify(vm.model), null, 2); } } }); })(); 
 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> <!-- jQuery --> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <!-- Twitter bootstrap --> <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> <!-- apiCheck is used by formly to validate its api --> <script src="//npmcdn.com/ api-check@latest /dist/api-check.js"></script> <!-- This is the latest version of angular (at the time this template was created) --> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script> <!-- This is the latest version of formly core. --> <script src="//npmcdn.com/ angular-formly@latest /dist/formly.js"></script> <!-- This is the latest version of formly bootstrap templates --> <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> <!-- Moment --> <script src="https://cdn.rawgit.com/moment/moment/develop/min/moment-with-locales.min.js"></script> <!-- Datetime picker --> <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> <!-- Put custom templates here --> <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> 
+3
source

You must return your validation to formlyConfigProvider without passing it as a value for templateOptions . Return

 validation: { show: true } 

instead

 templateOptions: { validation: { show: true } } 

Your formlyConfigProvider should look something like this:

 formlyConfigProvider.setType({ name: 'datepicker', templateUrl: "custom-template.html", overwriteOk: true, wrapper: ['bootstrapHasError'], defaultOptions: function defaultOptions(options) { return { validation: { show: true } }; } }); 

Here is the JSBin for working code.

0
source

Source: https://habr.com/ru/post/1013189/


All Articles