Skip to content

Commit

Permalink
validations for doi model. #280
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jan 5, 2020
1 parent 7fe061f commit d6bfb7a
Show file tree
Hide file tree
Showing 24 changed files with 113 additions and 157 deletions.
3 changes: 3 additions & 0 deletions app/components/doi-titles.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export default Component.extend({
didReceiveAttrs() {
this._super(...arguments);

if (!this.model.get('titles')) {
this.model.set('titles', []);
}
if (this.model.get('titles').length == 0) {
this.model.get('titles').createFragment();
}
Expand Down
11 changes: 11 additions & 0 deletions app/components/model-validation-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ export default Component.extend({
if (this.model.get('state') !== 'draft') {
let errors = this.model.get('validations.errors');

if (this.model.titles) {
this.model.titles.forEach((title) => {
errors = errors.concat(title.validations.errors);
});
}
if (this.model.creators) {
this.model.creators.forEach((creator) => {
errors = errors.concat(creator.validations.errors);
});
}

if (errors.length > 0) {
A(errors).forEach((item) => {
console.log(item);
Expand Down
17 changes: 6 additions & 11 deletions app/controllers/repositories/show/dois/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ export default Controller.extend({
// don't send xml
doi.set('xml', null);

// only store descriptions that have a description text
doi.set('descriptions', A(doi.get('descriptions')).filter(function(description) {
return !isBlank(description.description);
}));

// only store name identifiers and affiliations with a value
A(doi.get('creators')).forEach((creator) => {
creator.set('nameIdentifiers', A(creator.get('nameIdentifiers')).filter(function(nameIdentifier) {
Expand Down Expand Up @@ -64,11 +59,11 @@ export default Controller.extend({
cancel() {
this.transitionToRoute('repositories.show.dois', this.get('model.repository.id'));
},
setCreatorValidations(value) {
console.log(value);
},
setTitleValidations(value) {
console.log(value);
},
// setCreatorValidations(value) {
// console.log(value);
// },
// setTitleValidations(value) {
// console.log(value);
// },
},
});
28 changes: 28 additions & 0 deletions app/helpers/form-has-errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { helper } from '@ember/component/helper';

export function formHasErrors([ model ]) {
if (model.state === 'draft') {
return true;
}

let invalidModel = model.validations.errors.length > 0;

// check validation errors for embed data model fragments
let invalidTitle = false;
let invalidCreator = false;

if (model.titles) {
invalidTitle = model.titles.any(title => {
return title.validations.errors.length > 0;
});
}
if (model.creators) {
invalidCreator = model.creators.any(creator => {
return creator.validations.errors.length > 0;
});
}

return invalidModel || invalidTitle || invalidCreator;
}

export default helper(formHasErrors);
15 changes: 0 additions & 15 deletions app/helpers/model-disabled.js

This file was deleted.

11 changes: 1 addition & 10 deletions app/models/affiliation.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import DS from 'ember-data';
import Fragment from 'ember-data-model-fragments/fragment';
import { validator, buildValidations } from 'ember-cp-validations';

const Validations = buildValidations({
'title': [
validator('presence', {
presence: true,
}),
],
});

export default Fragment.extend(Validations, {
export default Fragment.extend({
name: DS.attr('string'),
affiliationIdentifier: DS.attr('string', { defaultValue: null }),
affiliationIdentifierScheme: DS.attr('string', { defaultValue: null }),
Expand Down
16 changes: 3 additions & 13 deletions app/models/creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ import { computed } from '@ember/object';
import { A } from '@ember/array';

const Validations = buildValidations({
'name': [
name: [
validator('presence', {
presence: true,
disabled: computed('model.nameType', function() {
// only validate if nameType is not "Personal"
this.model.get('nameType') === 'Personal';
}),
}),
],
'givenName': [
givenName: [
validator('presence', {
presence: true,
disabled: computed('model.nameType', function() {
Expand All @@ -23,7 +19,7 @@ const Validations = buildValidations({
}),
}),
],
'familyName': [
familyName: [
validator('presence', {
presence: true,
disabled: computed('model.nameType', function() {
Expand All @@ -32,12 +28,6 @@ const Validations = buildValidations({
}),
}),
],
'nameIdentifiers': [
validator('has-many'),
],
// 'affiliation': [
// validator('has-many')
// ]
});

export default MF.Fragment.extend(Validations, {
Expand Down
2 changes: 1 addition & 1 deletion app/models/description.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { validator, buildValidations } from 'ember-cp-validations';
import { computed } from '@ember/object';

const Validations = buildValidations({
'description': [
description: [
validator('format', {
regex: /^([^,]+)(, \w+(\(\w+\))?, \w+(-\w+)?)?$/,
message: 'Series information not in recommended format of series title, followed by comma and optional volume(issue), firstpage-lastpage',
Expand Down
17 changes: 4 additions & 13 deletions app/models/doi.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ const Validations = buildValidations({
}),
}),
],
'creators': [
validator('has-many'),
],
publisher: [
validator('presence', {
presence: true,
Expand Down Expand Up @@ -104,22 +101,19 @@ const Validations = buildValidations({
'types.resourceTypeGeneral': [
validator('presence', {
presence: true,
isWarning: computed('model.state', 'model.prefix', function() {
isWarning: computed('model.state', function() {
return this.get('model.state') === 'draft';
}),
disabled: computed('model.mode', function() {
return ![ 'new', 'edit' ].includes(this.model.get('mode'));
}),
}),
],
'titles': [
validator('has-many'),
],
xml: [
validator('presence', {
presence: true,
message: 'Please include valid metadata.',
disabled: computed('model.mode', 'model.state', 'model.prefix', function() {
disabled: computed('model.mode', 'model.state', function() {
return ![ 'upload', 'modify' ].includes(this.model.get('mode')) || this.get('model.state') === 'draft';
}),
}),
Expand All @@ -134,9 +128,6 @@ const Validations = buildValidations({
}),
}),
],
'descriptions': [
validator('has-many'),
],
});

export default DS.Model.extend(Validations, {
Expand All @@ -151,8 +142,8 @@ export default DS.Model.extend(Validations, {
suffix: DS.attr('string'),
url: DS.attr('string'),
contentUrl: DS.attr(),
creators: fragmentArray('creator'),
titles: fragmentArray('title'),
creators: fragmentArray('creator', { defaultValue: [] }),
titles: fragmentArray('title', { defaultValue: [] }),
publisher: DS.attr('string'),
bcontainer: DS.attr(),
publicationYear: DS.attr('number'),
Expand Down
14 changes: 7 additions & 7 deletions app/models/name-identifier.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import DS from 'ember-data';
import Fragment from 'ember-data-model-fragments/fragment';
import { validator, buildValidations } from 'ember-cp-validations';
// import { validator, buildValidations } from 'ember-cp-validations';

const Validations = buildValidations({
'nameIdentifier': [
validator('name-identifier', true),
],
});
// const Validations = buildValidations({
// nameIdentifier: [
// validator('name-identifier', true),
// ],
// });

export default Fragment.extend(Validations, {
export default Fragment.extend({
nameIdentifier: DS.attr('string'),
nameIdentifierScheme: DS.attr('string'),
schemeUri: DS.attr('string'),
Expand Down
2 changes: 1 addition & 1 deletion app/models/title.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Fragment from 'ember-data-model-fragments/fragment';
import { validator, buildValidations } from 'ember-cp-validations';

const Validations = buildValidations({
'title': [
title: [
validator('presence', {
presence: true,
}),
Expand Down
37 changes: 7 additions & 30 deletions app/templates/components/doi-creator.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,14 @@
{{#if showPersonal}}
<div>
<label for="givenName" class="subtitle">Given Name</label>
<input type="text" class="form-control given-name-field {{if (not (v-get fragment 'givenName' 'messages')) 'no-error'}}" value={{fragment.givenName}} oninput={{action "updateGivenName" value="target.value"}} readonly={{isReadonlyNameParts}} data-test-given-name/>

{{#if (v-get fragment 'givenName' 'errors')}}
<div class="help-block help-block-fragment">{{v-get fragment 'givenName' 'message'}}</div>
{{else if (v-get fragment 'givenName' 'warnings')}}
<div class="help-block help-block-fragment">{{v-get fragment 'givenName' 'warningMessage'}}</div>
{{else}}
<div class="help-block help-block-fragment no-error">The personal or first name of the creator.</div>
{{/if}}
<input type="text" class="form-control given-name-field" value={{fragment.givenName}} oninput={{action "updateGivenName" value="target.value"}} readonly={{isReadonlyNameParts}} data-test-given-name/>
<div class="help-block help-block-fragment">The personal or first name of the creator.</div>
</div>

<div>
<label for="familyName" class="subtitle">Family Name</label>
<input type="text" class="form-control family-name-field {{if (not (v-get fragment 'familyName' 'messages')) 'no-error'}}" value={{fragment.familyName}} oninput={{action "updateFamilyName" value="target.value"}} readonly={{isReadonlyNameParts}} data-test-family-name/>

{{#if (v-get fragment 'familyName' 'errors')}}
<div class="help-block help-block-fragment">{{v-get fragment 'familyName' 'message'}}</div>
{{else if (v-get fragment 'familyName' 'warnings')}}
<div class="help-block help-block-fragment">{{v-get fragment 'familyName' 'warningMessage'}}</div>
{{else}}
<div class="help-block help-block-fragment no-error">The surname or last name of the creator.</div>
{{/if}}
<input type="text" class="form-control family-name-field" value={{fragment.familyName}} oninput={{action "updateFamilyName" value="target.value"}} readonly={{isReadonlyNameParts}} data-test-family-name/>
<div class="help-block help-block-fragment">The surname or last name of the creator.</div>
</div>

<div>
Expand All @@ -73,18 +59,9 @@

<div class="help-block help-block-fragment no-error no-success">The main researchers involved in producing the data, or the authors of the publication, in priority order.</div>
{{else}}
<div>
<label for="name" class="subtitle">Name</label>
</div>
<input type="text" class="form-control name-field {{if (not (v-get fragment 'name' 'messages')) 'no-error'}}" value={{fragment.name}} oninput={{action "updateName" value="target.value"}} readonly={{isReadonly}}/>

{{#if (v-get fragment 'name' 'errors')}}
<div class="help-block help-block-fragment">{{v-get fragment 'name' 'message'}}</div>
{{else if (v-get fragment 'name' 'warnings')}}
<div class="help-block help-block-fragment">{{v-get fragment 'name' 'warningMessage'}}</div>
{{else}}
<div class="help-block help-block-fragment no-error">The main organizations involved in producing the data, or the authors of the publication, in priority order.</div>
{{/if}}
<div><label for="name" class="subtitle">Name</label></div>
<input type="text" class="form-control name-field" value={{fragment.name}} oninput={{action "updateName" value="target.value"}} readonly={{isReadonly}} data-test-name/>
<div class="help-block help-block-fragment no-error">The main organizations involved in producing the data, or the authors of the publication, in priority order.</div>
{{/if}}
{{/if}}

Expand Down
6 changes: 2 additions & 4 deletions app/templates/components/doi-description.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
<textarea class="form-control description-field {{if (not isSeriesInformation) 'no-error'}}" rows="8" style="resize:none" value={{fragment.description}}
oninput={{action "updateDescription" value="target.value"}} data-test-description/>
{{/if}}
{{#if (v-get fragment 'description' 'warnings')}}
<div class="help-block help-block-fragment description-field">{{v-get fragment 'description' 'warningMessage'}}</div>
{{else if isSeriesInformation}}
{{#if isSeriesInformation}}
<div class="help-block help-block-fragment description-field">Enter series information in the recommended format of series title, followed by comma and optional volume(issue), firstpage-lastpage</div>
{{else}}
<div class="help-block help-block-fragment description-field no-error">All additional information that does not fit in any of the other categories.</div>
<div class="help-block help-block-fragment description-field">All additional information that does not fit in any of the other categories.</div>
{{/if}}

<div class="power-select-label">
Expand Down
16 changes: 5 additions & 11 deletions app/templates/components/doi-name-identifier.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,10 @@
</span>
</div>
{{else}}
<input type="text" class="form-control name-identifier-field {{if (not (v-get fragment 'nameIdentifier' 'messages')) 'no-error'}} {{if (not fragment.nameIdentifier) 'no-success'}}" value={{fragment.nameIdentifier}} oninput={{action "updateNameIdentifier" value="target.value"}} data-test-name-identifier/>
<input type="text" class="form-control name-identifier-field" value={{fragment.nameIdentifier}} oninput={{action "updateNameIdentifier" value="target.value"}} data-test-name-identifier/>

{{#if (v-get fragment 'nameIdentifier' 'errors')}}
<div class="help-block help-block-fragment name-identifier-field">{{v-get fragment 'nameIdentifier' 'message'}}</div>
{{else if (v-get fragment 'nameIdentifier' 'warnings')}}
<div class="help-block help-block-fragment name-identifier-field">{{v-get fragment 'nameIdentifier' 'warningMessage'}}</div>
{{else}}
<div class="help-block help-block-fragment name-identifier-field no-error {{if (not fragment.nameIdentifier) 'no-success'}}">
Uniquely identifies an individual or legal entity, according to various schemas, e.g. ORCID, ROR or ISNI.
Use name identifier expressed as URL. The Given Name, Family Name and Name will automatically be filled out for ORCID and ROR identifiers.
</div>
{{/if}}
<div class="help-block help-block-fragment name-identifier-field no-error {{if (not fragment.nameIdentifier) 'no-success'}}">
Uniquely identifies an individual or legal entity, according to various schemas, e.g. ORCID, ROR or ISNI.
Use name identifier expressed as URL. The Given Name, Family Name and Name will automatically be filled out for ORCID and ROR identifiers.
</div>
{{/if}}
13 changes: 3 additions & 10 deletions app/templates/components/doi-title.hbs
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
{{#if (gt index 0)}}
<div class="input-group">
<input type="text" class="form-control title-field no-error no-success" value={{fragment.title}} oninput={{action "updateTitle" value="target.value"}} data-test-title/>
<input type="text" class="form-control title-field" value={{fragment.title}} oninput={{action "updateTitle" value="target.value"}} data-test-title/>
<span class="input-group-btn">
<BsButton @outline={{true}} @onClick={{action "deleteTitle" index}}>{{fa-icon "trash"}}</BsButton>
</span>
</div>
<div class="help-block help-block-fragment no-error no-success">One or more names or titles by which the resource is known.</div>
<div class="help-block help-block-fragment">One or more names or titles by which the resource is known.</div>
{{else}}
<input type="text" class="form-control title-field" value={{fragment.title}} oninput={{action "updateTitle" value="target.value"}} data-test-title/>

{{#if (v-get fragment 'title' 'errors')}}
<div class="help-block help-block-fragment">{{v-get fragment 'title' 'message'}}</div>
{{else if (v-get fragment 'title' 'warnings')}}
<div class="help-block help-block-fragment">{{v-get fragment 'title' 'warningMessage'}}</div>
{{else}}
<div class="help-block help-block-fragment">One or more names or titles by which the resource is known.</div>
{{/if}}
<div class="help-block help-block-fragment">One or more names or titles by which the resource is known.</div>
{{/if}}

<div class="power-select-label">
Expand Down
2 changes: 1 addition & 1 deletion app/templates/dois/show/edit.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<ModelValidationErrors @model={{model}} @form={{form}} />

<div class="col-md-9 col-md-offset-3">
<button type="submit" id="doi-update" class="btn btn-sm btn-fill" disabled={{and (v-get model 'isInvalid') (not-eq model.state 'draft')}}>Update DOI</button>
<button type="submit" id="doi-update" class="btn btn-sm btn-fill" disabled={{form-has-errors model}}>Update DOI</button>
<button {{action "cancel"}} class="btn btn-sm">Cancel</button>
</div>
</BsForm>
Expand Down
2 changes: 1 addition & 1 deletion app/templates/dois/show/modify.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<ModelValidationErrors @model={{model}} @form={{form}} />

<div class="col-md-9 col-md-offset-3">
<button type="submit" id="doi-modify" class="btn btn-sm btn-fill" disabled={{and (v-get model 'isInvalid') (not-eq model.state 'draft')}}>Update DOI</button>
<button type="submit" id="doi-modify" class="btn btn-sm btn-fill" disabled={{form-has-errors model}}>Update DOI</button>
<button {{action "cancel"}} class="btn btn-sm">Cancel</button>
</div>
</BsForm>
Expand Down
Loading

0 comments on commit d6bfb7a

Please sign in to comment.