Currently, I am working on a project where there are multiple submit forms with similar fields. To streamline the process, I have made the decision to transfer some fields to directives.
Here is an example of the form:
<div loading class="col-sm-12" ng-controller="Controller">
<form name="myForm" role="form" novalidate>
<fieldset class="lines-header-border">
<div class="col-sm-6">
<div class="form-group">
<label class="col-sm-4 control-label">Date</label>
<div class="col-sm-7">
<date-field
model="myModel"
date-picker-options="datePickerOptions"
for-name="date"
for-ng-class="myForm.date.$invalid"
is-required="true"/>
</div>
<div class="col-sm-4">
<input class="form-control"
name="amount"
ng-model="amount"
ng-class="{ 'has-error' : myForm.amount.$invalid }"
required/>
</div>
</div>
</fieldset>
</form>
</div>
Date field directive implementation:
.directive('dateField', function() {
return {
restrict: 'E',
replace: true,
scope: {
model: '=',
datePickerOptions: '=',
isRequired: '@',
forName:'@',
forNgClass: '&'
},
template: '<input ui-date="datePickerOptions" type="date" name="{{forName}}" class="form-control" ng-model="model" ng-class="{ \'has-error\' : forNgClass()}" ng-required="isRequired"/>'
};
});
Issue with validation during form submission: While other fields are being validated correctly upon form submission, the date field does not seem to be validating. I suspect that the problem lies in
for-ng-class="myForm.date.$invalid"
.
Any suggestions on how to address this issue?