diff --git a/models/Component.cfc b/models/Component.cfc index 127a63f..84e0267 100644 --- a/models/Component.cfc +++ b/models/Component.cfc @@ -924,6 +924,33 @@ component output="true" accessors="true" { ); } + /** + * Method that is invoked when a file upload errors. + * + * @prop string | The property for the file input. + * @errors any | The errors that occurred during upload. + * @multiple boolean | Whether multiple files are being uploaded. + * + * @return void + */ + function _uploadErrored( prop, errors, multiple ) { + // Dispatch the upload errored event + dispatchSelf( + event="upload:errored", + params=[ + "name"=arguments.prop + ] + ); + // Check if the component has an onUploadError method and invoke it + if ( structKeyExists( this, "onUploadError" ) ) { + invoke( this, "onUploadError", { + property: arguments.prop, + errors: arguments.errors, + multiple: arguments.multiple + } ); + } + } + /** * Fires when missing methods are called. * Handles computed properties. diff --git a/test-harness/tests/specs/CBWIRESpec.cfc b/test-harness/tests/specs/CBWIRESpec.cfc index 37a4ae4..158e5fb 100644 --- a/test-harness/tests/specs/CBWIRESpec.cfc +++ b/test-harness/tests/specs/CBWIRESpec.cfc @@ -1060,6 +1060,35 @@ component extends="coldbox.system.testing.BaseTestCase" { expect( response.components[1].effects.html ).toInclude( "Hydrated Property: true" ); } ); + it( "should call onUploadError() if it exists when _uploadErrored is called", function() { + var payload = incomingRequest( + memo = { + "name": "test.should_call_onuploaderror", + "id": "Z1Ruz1tGMPXSfw7osBW2", + "children": [] + }, + data = { + "uploadErrored": false, + "erroredPropertyName": "", + "errorInfo": "", + "isMultiple": false + }, + calls = [ + { + "path": "", + "method": "_uploadErrored", + "params": [ "photo", null, false ] + } + ], + updates = {} + ); + var response = cbwireController.handleRequest( payload, event ); + expect( response.components[1].effects.html ).toInclude( "Upload Errored: true" ); + expect( response.components[1].effects.html ).toInclude( "Errored Property Name: photo" ); + expect( response.components[1].effects.html ).toInclude( "Error Info: null" ); + expect( response.components[1].effects.html ).toInclude( "Is Multiple: false" ); + } ); + it( "should be able to return javascript to return", () => { var payload = incomingRequest( memo = { diff --git a/test-harness/wires/OnUploadError.cfc b/test-harness/wires/OnUploadError.cfc new file mode 100644 index 0000000..e5df5b5 --- /dev/null +++ b/test-harness/wires/OnUploadError.cfc @@ -0,0 +1,17 @@ +component extends="cbwire.models.Component" { + + data = { + "uploadErrored": false, + "erroredPropertyName": "", + "errorInfo": "", + "isMultiple": false + }; + + function onUploadError( property, errors, multiple ) { + data.uploadErrored = true; + data.erroredPropertyName = arguments.property; + data.errorInfo = isNull( arguments.errors ) ? "null" : "has errors"; + data.isMultiple = arguments.multiple; + } + +} diff --git a/test-harness/wires/OnUploadError.cfm b/test-harness/wires/OnUploadError.cfm new file mode 100644 index 0000000..b78dbf4 --- /dev/null +++ b/test-harness/wires/OnUploadError.cfm @@ -0,0 +1,8 @@ + + + Upload Errored: #uploadErrored# + Errored Property Name: #erroredPropertyName# + Error Info: #errorInfo# + Is Multiple: #isMultiple# + + diff --git a/test-harness/wires/test/should_call_onuploaderror.cfm b/test-harness/wires/test/should_call_onuploaderror.cfm new file mode 100644 index 0000000..247f311 --- /dev/null +++ b/test-harness/wires/test/should_call_onuploaderror.cfm @@ -0,0 +1,26 @@ + + // @startWire + data = { + "uploadErrored": false, + "erroredPropertyName": "", + "errorInfo": "", + "isMultiple": false + }; + + function onUploadError( property, errors, multiple ) { + data.uploadErrored = true; + data.erroredPropertyName = arguments.property; + data.errorInfo = isNull( arguments.errors ) ? "null" : "has errors"; + data.isMultiple = arguments.multiple; + } + // @endWire + + + + + Upload Errored: #uploadErrored# + Errored Property Name: #erroredPropertyName# + Error Info: #errorInfo# + Is Multiple: #isMultiple# + +