Skip to content

Commit

Permalink
Validation checking for required attached documents
Browse files Browse the repository at this point in the history
  • Loading branch information
horatiorosa committed Nov 29, 2024
1 parent 7b5b9bf commit db2fb6d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
@attribute='documents'
@validation={{@form.errors.documents.validation}}
/>

<Packages::Attachments
@package={{@form.data}}
@fileManager={{@model.fileManager}}
Expand Down
12 changes: 12 additions & 0 deletions client/app/components/projects/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { inject as service } from '@ember/service';
import SubmittableProjectsNewForm from '../../validations/submittable-projects-new-form';
import { optionset } from '../../helpers/optionset';
import config from '../../config/environment';
import validateFileUpload from '../../validators/validate-file-presence';

export default class ProjectsNewFormComponent extends Component {
validations = {
Expand Down Expand Up @@ -42,6 +43,17 @@ export default class ProjectsNewFormComponent extends Component {

const contactInputs = [primaryContactInput, applicantInput];

const validationResult = validateFileUpload(
{
message: 'Please upload at least one file before submitting.',
},
)('documents', this.args.package.documents);

if (validationResult !== true) {
this.errorMessage = validationResult;
return;
}

try {
const contactPromises = contactInputs.map((contact) => this.store.queryRecord('contact', {
email: contact.email,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
@package={{@form.data}}
@artifact={{@artifact}}
@fileManager={{@model.fileManager}}
@attribute="documents"
data-test-section='attachments' />
</@form.Section>

Expand Down
16 changes: 15 additions & 1 deletion client/app/components/projects/projects-new-attachments.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Component from '@glimmer/component';
import { action } from '@ember/object';
import validateFileUpload from '../../validators/validate-file-presence';

/**
* This component wires a fileManager to the attachments UI.
Expand Down Expand Up @@ -30,7 +31,6 @@ export default class ProjectsNewAttachmentsComponent extends Component {
// upload to update the fileManager isDirty state.
@action
trackFileForUpload() {
/* eslint-disable-next-line no-console */
this.fileManager.trackFileForUpload();
this.args.package.documents = [
...this.args.package.documents,
Expand All @@ -46,4 +46,18 @@ export default class ProjectsNewAttachmentsComponent extends Component {
(document) => document !== file,
);
}

@action
validateFilePresence() {
const validationResult = validateFileUpload({ message: 'One or more document uploads is required.' })(
'documents',
this.fileManager.filesToUpload.files,
);

if (validationResult !== true) {
this.errorMessage = validationResult;
} else {
this.errorMessage = null;
}
}
}
6 changes: 6 additions & 0 deletions client/app/validations/submittable-projects-new-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
validateLength,
validatePresence,
} from 'ember-changeset-validations/validators';
import validateFileUpload from '../validators/validate-file-presence';

export default {
primaryContactFirstName: [
Expand Down Expand Up @@ -137,4 +138,9 @@ export default {
message: 'This field is required',
}),
],
documents: [
validateFileUpload({
message: 'One or more document uploads is required.',
}),
],
};
9 changes: 9 additions & 0 deletions client/app/validators/validate-file-presence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function validateFileUpload(options = {}) {
return (key, newValue) => {
if (!newValue || newValue.length === 0) {
return options.message || `${key} one or more document uploads is required.`;
}

return true;
};
}

0 comments on commit db2fb6d

Please sign in to comment.