Fixes #3309 - Add same styles as regular input elements to file inputs. Add test for validating file type.

This commit is contained in:
Owen Conti
2017-07-03 18:04:49 -06:00
parent b2b9e84e11
commit dd1b87e3d7
3 changed files with 40 additions and 4 deletions

View File

@@ -462,6 +462,12 @@ export const validateInteger = ( val ) => {
}
}
export const validateFile = ( val ) => {
if ( !(val instanceof win.File) ) {
return "Value must be a file"
}
}
// validation of parameters before execute
export const validateParam = (param, isXml) => {
let errors = []
@@ -472,7 +478,9 @@ export const validateParam = (param, isXml) => {
let stringCheck = type === "string" && !value
let arrayCheck = type === "array" && Array.isArray(value) && !value.length
let listCheck = type === "array" && Im.List.isList(value) && !value.count()
if ( required && (stringCheck || arrayCheck || listCheck) ) {
let fileCheck = type === "file" && !(value instanceof win.File)
if ( required && (stringCheck || arrayCheck || listCheck || fileCheck) ) {
errors.push("Required field is not provided")
return errors
}
@@ -505,7 +513,10 @@ export const validateParam = (param, isXml) => {
errors.push({ index: index, error: err})
}
})
} else if ( type === "file" ) {
let err = validateFile(value)
if (!err) return errors
errors.push(err)
}
return errors

View File

@@ -42,7 +42,8 @@ label
input[type=text],
input[type=password],
input[type=search],
input[type=email]
input[type=email],
input[type=file]
{
min-width: 100px;
margin: 5px 0;

View File

@@ -1,7 +1,8 @@
/* eslint-env mocha */
import expect from "expect"
import { fromJS } from "immutable"
import { mapToList, validateNumber, validateInteger, validateParam } from "core/utils"
import { mapToList, validateNumber, validateInteger, validateParam, validateFile } from "core/utils"
import win from "core/window"
describe("utils", function(){
@@ -157,6 +158,19 @@ describe("utils", function(){
})
})
describe("validateFile", function() {
let errorMessage = "Value must be a file"
it("validates against objects which are instances of 'File'", function() {
let fileObj = new win.File([], "Test File")
expect(validateFile(fileObj)).toBeFalsy()
expect(validateFile(null)).toEqual(errorMessage)
expect(validateFile(undefined)).toEqual(errorMessage)
expect(validateFile(1)).toEqual(errorMessage)
expect(validateFile("string")).toEqual(errorMessage)
})
})
describe("validateParam", function() {
let param = null
let result = null
@@ -171,6 +185,16 @@ describe("utils", function(){
expect( result ).toEqual( ["Required field is not provided"] )
})
it("validates required files", function() {
param = fromJS({
required: true,
type: "file",
value: undefined
})
result = validateParam( param, false )
expect( result ).toEqual( ["Required field is not provided"] )
})
it("validates required arrays", function() {
param = fromJS({
required: true,