Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f88f2ba

Browse files
committedJan 6, 2016
Merge pull request #638 from appirio-tech/SUP-2919-file-validation
Add validation and error handling for file inputs
2 parents 1ed49ad + 53d1b49 commit f88f2ba

File tree

3 files changed

+63
-19
lines changed

3 files changed

+63
-19
lines changed
 

‎app/directives/tc-file-input/tc-file-input.directive.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,25 @@
66
function tcFileInput() {
77
return {
88
restrict: 'E',
9-
require: '^form',
9+
require: ['^form', '^ngModel'],
1010
templateUrl: 'directives/tc-file-input/tc-file-input.html',
1111
scope: {
1212
labelText: '@',
1313
fieldId: '@',
1414
placeholder: '@',
1515
fileType: '@',
16+
showFileType: '=',
1617
mandatory: '=',
1718
buttonText: '@',
1819
setFileReference: '&',
19-
fileValue: '=ngModel'
20+
ngModel: '='
2021
},
21-
link: function(scope, element, attrs, formController) {
22+
link: function(scope, element, attrs, controllers) {
23+
var formController = controllers[0];
24+
var ngModel = controllers[1];
25+
2226
scope.selectFile = selectFile;
27+
var fileTypes = scope.fileType.split(',');
2328

2429
// fieldId is not set on element at this point, so grabbing with class .none
2530
// which exists on the element right away
@@ -29,12 +34,28 @@
2934
fileInput.bind('change', function() {
3035
var file = fileInput[0].files[0];
3136

32-
// Pass file object up through callback into controller
33-
scope.setFileReference({file: file, fieldId: scope.fieldId});
37+
var selectedFileType = file.type.slice(file.type.lastIndexOf('/') + 1);
38+
var isAllowedFileFormat = _.some(fileTypes, _.matches(selectedFileType));
39+
40+
scope.$apply(function(){
41+
if (!isAllowedFileFormat) {
42+
fileNameInput[0].value = file.name;
43+
44+
// Manually setting is required since Angular doesn't support file inputs
45+
formController[attrs.fieldId].$setTouched();
46+
formController[attrs.fieldId].$setValidity('required', false);
47+
48+
} else {
49+
// Pass file object up through callback into controller
50+
scope.setFileReference({file: file, fieldId: scope.fieldId});
51+
52+
// Set the file name as the value of the disabled input
53+
fileNameInput[0].value = file.name;
3454

35-
// Set the file name as the value of the disabled input
36-
fileNameInput[0].value = file.name;
37-
formController[attrs.fieldId].$setValidity('required', true);
55+
// Manually set validity of specific input field
56+
formController[attrs.fieldId].$setValidity('required', true);
57+
}
58+
});
3859
});
3960

4061
function selectFile() {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
.tc-file-field__label
22
label.tc-label {{labelText}}
3-
span.lowercase(ng-if="fileType") {{fileType | addBeginningSpace}}
3+
span.lowercase(ng-if="showFileType") {{ ' *(.' + fileType + ')'}}
44

55
span.tc-label__mandatory.lowercase(ng-if="mandatory") #[span *]mandatory
66

77
.tc-file-field__inputs
8-
input.tc-file-field__input(type="text", placeholder="{{placeholder}}")
8+
input.tc-file-field__input(type="text", placeholder="{{placeholder}}", disabled)
99

1010
button.tc-btn(ng-click="selectFile()") {{buttonText}}
1111

12-
input.none(name="{{fieldId}}", type="file", id="{{fieldId}}", required, ng-model="fileValue")
12+
input.none(name="{{fieldId}}", type="file", required, ng-model="ngModel")

‎app/submissions/submit-file/submit-file.jade

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,56 @@
2929
label-text="Submission",
3030
field-id="SUBMISSION_ZIP",
3131
button-text="Add File",
32-
file-type="(*.zip)",
32+
file-type="zip",
33+
show-file-type="true",
3334
placeholder="Attach all visible files as a single .zip file",
3435
mandatory="true",
3536
set-file-reference="vm.setFileReference(file, fieldId)",
36-
file-value="vm.submissionForm.submissionZip"
37+
ng-model="vm.submissionForm.submissionZip"
3738
)
3839

40+
.tc-error-messages(ng-show="submissionForm['SUBMISSION_ZIP'].$touched && submissionForm['SUBMISSION_ZIP'].$invalid")
41+
p(ng-show="submissionForm['SUBMISSION_ZIP'].$error.required") This is not the correct file format. Please select a .zip file.
42+
3943
tc-file-input.tc-file-field(
4044
label-text="Source",
4145
field-id="SOURCE_ZIP",
4246
button-text="Add File",
43-
file-type="(*.zip)",
47+
file-type="zip",
48+
show-file-type="true",
4449
placeholder="Attach all source files as a single .zip file",
4550
mandatory="true",
4651
set-file-reference="vm.setFileReference(file, fieldId)",
47-
file-value="vm.submissionForm.sourceZip"
52+
ng-model="vm.submissionForm.sourceZip"
4853
)
4954

55+
.tc-error-messages(ng-show="submissionForm['SOURCE_ZIP'].$touched && submissionForm['SOURCE_ZIP'].$invalid")
56+
p(ng-show="submissionForm['SOURCE_ZIP'].$error.required") This is not the correct file format. Please select a .zip file.
57+
5058
tc-file-input.tc-file-field(
5159
label-text="Preview Image",
5260
field-id="DESIGN_COVER",
5361
button-text="Add File",
62+
file-type="jpg,jpeg,png"
5463
placeholder="Image file as .jpg or .png",
5564
mandatory="true",
5665
set-file-reference="vm.setFileReference(file, fieldId)",
57-
file-value="vm.submissionForm.designCover"
66+
ng-model="vm.submissionForm.designCover"
67+
)
68+
69+
.tc-error-messages(ng-show="submissionForm['DESIGN_COVER'].$touched && submissionForm['DESIGN_COVER'].$invalid")
70+
p(ng-show="submissionForm['DESIGN_COVER'].$error.required") This is not the correct file format. Please select a .jpg or .png file.
71+
72+
tc-input.fieldset__input.submitterRank(
73+
label-text="Rank #",
74+
input-name="Submission_Rank",
75+
input-value="vm.submissionForm.submitterRank",
76+
input-pattern="vm.rankRegEx",
77+
update-value-on-blur="vm.setRankTo1(inputValue)"
5878
)
5979

60-
tc-input.fieldset__input.submitterRank(label-text="Rank #", input-value="vm.submissionForm.submitterRank")
80+
.tc-error-messages(ng-show="submissionForm.Submission_Rank.$dirty && submissionForm.Submission_Rank.$invalid")
81+
p(ng-show="submissionForm.Submission_Rank.$error.pattern") Please enter a positive integer.
6182

6283
.form-block.flex.wrap
6384
.form-block__instructions
@@ -104,13 +125,15 @@
104125
tc-input.fieldset__input(
105126
label-text="Font Name",
106127
placeholder="Select font source to edit field"
107-
input-value="font.name"
128+
input-value="font.name",
129+
input-name="fontName{{$index}}"
108130
)
109131

110132
tc-input.fieldset__input(
111133
label-text="Font URL",
112134
placeholder="Select font source to edit field",
113-
input-value="font.sourceUrl"
135+
input-value="font.sourceUrl",
136+
input-name="fontUrl{{$index}}"
114137
)
115138

116139
button.fieldset__button.tc-btn.tc-btn-s(type="button", ng-click="vm.createAnotherFontFieldset()") + Add Font

0 commit comments

Comments
 (0)
This repository has been archived.