RockefellerArchiveCenter/aquila

Add validation for dates

Closed this issue · 7 comments

Is your feature request related to a problem? Please describe.

In local practice, PREMIS rights bases and acts must include a start date, and optionally an end date. Since dates can either be a hard coded date (e.g., 1/1/1974) or a number of years after creation of the item they're being applied to (or, for end dates only, "open"), this requirement is not being enforced at the model level and should be enforced when the user is submitting a form.

Describe the solution you'd like

Before a rights shell is saved, make sure that the following conditions are met for basis dates and any act dates:

  • There is one and only one start date, either a calendar date or a number of years after creation
  • There is no more than one end date, including the "open end date" boolean
  • The end date is not before the start date

Additional context

This likely needs to be done in Vue in manage.html and not the forms, but I'm not 100% certain of the best approach.

I'm trying to do the validation in Vue, but getting stuck on 1) how to do the actual validation (in terms of when validation happens, and what happens with the errors) 2) how to deal with Vue variables (I did get as far as adding the v-model attributes to the relevant form field widgets). I have this method so far (modeled on this example), but I'm not sure of the cleanest way of dealing with complex validation in Vue/JS methods:

checkStartDates: function (e) {
  if ((!this.start_date && this.start_period) || (this.start_date && !this.start_period)) {
    return true;
  }

  if (!this.start_date && !this.start_period) {
    this.errors.push('Must have a start date or period.');
  }
  if (this.start_date  && this.start_period) {
    this.errors.push('Must have a start date or period.');
  }

}

@helrond Do you have any thoughts on the best approach?

In terms of how to wire things up, this looks like a useful primer: https://vuejs.org/v2/cookbook/form-validation.html. It looks like there's just a handler on the form submit.

I think you're on the right track with the validation. My read is that you're getting stuck with "Only one of start_date and start_date_period"? This looks relevant: https://codereview.stackexchange.com/questions/199853/function-to-test-if-exactly-one-of-three-parameters-is-truthy-without-using-equ and I think you could do

if !(!!this.start_date + !!this.start_period == 1) {
  this.errors.push('Must have only one of either a start date or start period')
}

Thanks @helrond! This is useful for making that method more compact. I'm still struggling with how to do all three validations (i.e., only one start, only one end, and comparing end and start values). Should those be three different methods or the same method? What do I actually do with the errors? Is on submit the best place to do validation, or should it happen earlier?

The answer to the first question depends on when you want to run validation. The easiest thing to do would be to run validation when you click submit, but you could provide more immediate feedback to a user to using an onBlur event handler or a watcher (see also this guide)

@bonniegee I don't know where you've got to with this issue, but you may want to take a look at #91 because...it may be that we can take another approach to how date fields are displayed to users, which might eliminate some of the need for this validation.

I did not get very far on this! I'm taking a look at that issue + the associated PR.

Since #93 fixes most of this issue, I'm closing this and opening a new issue (#100) for what is still outstanding.