我的代码工作正常,但是我正在寻找一种更好的方法来进行验证。我是angularjs的新手,需要帮助提高我的编码技能。
我在代码中有5个场景来验证所有场景。
首先,通过sourcing.startDate验证assignment.startDate
第二,assignment.startDate与采购.endDate
第三,assignment.endDate和Assignment.startDate
第四,assignment.endDate和purchase.startDate
第五,assignment.endDate与采购.endDate
$timeout(function () {
if (moment(vm.assignment.startDate) < moment(vm.procurement.startDate)) {
$scope.assignmentForm.startDate.$setValidity('mindate', false);
$scope.assignmentForm.startDate.$setTouched();
$scope.assignmentForm.startDate.$validate();
vm.endDateValidationMessage = "Start Date of Assignment Agreement cannot be earlier than Start Date of Procurement Agreement";
} else {
$scope.assignmentForm.startDate.$setValidity('mindate', true);
$scope.assignmentForm.startDate.$setTouched();
$scope.assignmentForm.startDate.$validate();
}
if (moment(vm.assignment.startDate) > moment(vm.procurement.endDate)) {
$scope.assignmentForm.startDate.$setValidity('maxdate', false);
$scope.assignmentForm.startDate.$setTouched();
$scope.assignmentForm.startDate.$validate();
vm.endDateValidationMessage = "Start Date of Assignment Agreement cannot be later than End Date of Procurement Agreement";
} else {
$scope.assignmentForm.startDate.$setValidity('maxdate', true);
$scope.assignmentForm.startDate.$setTouched();
$scope.assignmentForm.startDate.$validate();
}
if (moment(vm.assignment.endDate) < moment(vm.assignment.startDate)) {
$scope.assignmentForm.endDate.$setValidity('mindate', false);
$scope.assignmentForm.endDate.$setTouched();
$scope.assignmentForm.endDate.$validate();
vm.endDateValidationMessage = "End Date of Assignment Agreement cannot be earlier than Start Date of Assignment Agreement";
} else {
$scope.assignmentForm.endDate.$setValidity('mindate', true);
$scope.assignmentForm.endDate.$setTouched();
$scope.assignmentForm.endDate.$validate();
}
if (moment(vm.assignment.endDate) < moment(vm.procurement.startDate)) {
$scope.assignmentForm.endDate.$setValidity('maxdate', false);
$scope.assignmentForm.endDate.$setTouched();
$scope.assignmentForm.endDate.$validate();
vm.endDateValidationMessage = "End Date of Assignment Agreement cannot be earlier than Start Date of Procurement Agreement";
} else {
$scope.assignmentForm.endDate.$setValidity('maxdate', true);
$scope.assignmentForm.endDate.$setTouched();
$scope.assignmentForm.endDate.$validate();
}
if (moment(vm.assignment.endDate) > moment(vm.procurement.endDate)) {
$scope.assignmentForm.endDate.$setValidity('mindate', false);
$scope.assignmentForm.endDate.$setTouched();
$scope.assignmentForm.endDate.$validate();
vm.endDateValidationMessage = "End Date of Assignment Agreement cannot be later than End Date of Procurement Agreement";
} else {
$scope.assignmentForm.endDate.$setValidity('mindate', true);
$scope.assignmentForm.endDate.$setTouched();
$scope.assignmentForm.endDate.$validate();
}
})
<div>
<md-input-container class="md-block date-picker">
<md-datepicker md-no-asterisk placeholder="Start Date" name="startDate" ng-change="$ctrl.validateMinDate()"
md-min-date="$ctrl.procurement.startDate" md-max-date="$ctrl.procurement.endDate"
ng-required="true" md-open-on-focus ng-model="$ctrl.assignment.startDate"></md-datepicker>
<div class="error-wrap" ng-messages="assignmentForm.startDate.$error">
<div ng-message="required">Required.</div>
<div ng-message="mindate">Start Date of Assignment Agreement cannot be earlier than
Start Date of Procurement Agreement</div>
<div ng-message="maxdate">Start Date of Assignment Agreement cannot be after than
End Date of Procurement Agreement</div>
</div>
</md-input-container>
</div>
<div>
<md-input-container class="md-block date-picker">
<md-datepicker md-no-asterisk placeholder="End Date" name="endDate" ng-change="$ctrl.validateMinDate()"
ng-required="true" md-open-on-focus ng-model="$ctrl.assignment.endDate"></md-datepicker>
<div class="error-wrap" ng-messages="assignmentForm.endDate.$error">
<div ng-message="required">Required</div>
<div ng-message="mindate">{{$ctrl.endDateValidationMessage}}</div>
<div ng-message="maxdate">{{$ctrl.endDateValidationMessage}}</div>
</div>
</md-input-container>
</div>