Skip to content

Commit

Permalink
fix: #553 End of Operation Year Validation Fix. (#554)
Browse files Browse the repository at this point in the history
* Add conditional expression for @minDate; esentially, it becomes "equal or greater than" validator.

* Remove unused import

* Fix typo

* Deployment trigger commits.
  • Loading branch information
ianliuwk1019 authored Jan 9, 2024
1 parent 5b31e74 commit f66ddd6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
17 changes: 14 additions & 3 deletions admin/src/app/foms/fom-add-edit/fom-add-edit.form.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { AbstractControl } from '@angular/forms';
import { DistrictResponse, ForestClientResponse, ProjectResponse, WorkflowStateCode } from '@api-client';
import { minLength, prop, required, minDate, maxLength } from '@rxweb/reactive-form-validators';
import moment = require("moment");
import { maxLength, minDate, minLength, prop, required } from '@rxweb/reactive-form-validators';
import * as R from 'remeda';
import moment = require("moment");

const updateFields = [
'name',
Expand Down Expand Up @@ -57,7 +58,17 @@ export class FomAddEditForm implements Pick<ProjectResponse,

// Special case. It is at form control, but will be convert into request body for 'operationEndYear' (number).
@required({message: 'Operation End Year is required.'})
@minDate({fieldName:'opStartDate', message: 'Must be equal to or later than the Start of Operations'})
@minDate({
// In this case, do not use (x,y) arrow expression for validator.
// Use 'function(control)' expression, so it can get current field value through "this.".
conditionalExpression: function(control: AbstractControl) {
// For 'opStartDate' and 'opEndDate', only need "year" from the date; but still use "Date" type for datePicker.
// (This is a tricky case to set up "conditionalExpression" validator for @minDate, as date is passed from datePicker)
// So, conditionally, if years are the same, no need to validate on @minDate().
const sameYear = moment(this.opStartDate).year() == moment(this.opEndDate).year();
return !sameYear;
},
fieldName:'opStartDate', message: 'Must be equal to or later than the Start of Operations'})
@prop()
opEndDate: Date;

Expand Down
2 changes: 1 addition & 1 deletion public/src/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
@import "assets/styles/components/pagination.scss";
@import "assets/styles/components/spinner.scss";
@import "assets/styles/components/tab-nav.scss";
@import "assets/styles/components/table.scss";
@import "assets/styles/components/table.scss";

0 comments on commit f66ddd6

Please sign in to comment.