# Ad-hoc camunda-element creation issue

**URL:** <https://forum.camunda.io/t/ad-hoc-camunda-element-creation-issue/437>\
**Category:** Discussion & Questions\
**Tags:** cockpit, admin\
**Created:** [April 18, 2016, 7:52am UTC](https://forum.camunda.io/t/ad-hoc-camunda-element-creation-issue/437 "2016-04-18T07:52:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nvanbelle](https://forum.camunda.io/user_avatar/forum.camunda.io/nvanbelle/32/3452_2.png) [@nvanbelle](https://forum.camunda.io/u/nvanbelle)\
**Post date:** [April 18, 2016, 7:52am UTC](https://forum.camunda.io/t/ad-hoc-camunda-element-creation-issue/437/1 "2016-04-18T07:52:37Z")

</div>

Hi all,

I have to create a file upload component (custom directive) which has to look something like this:

![](https://forum.camunda.io/uploads/default/original/1X/bbe8bd37879d2e13308f48f58cb9cb912191e097.png)

Problem is that my upload-components which have following template are invalid so I cannot submit the form (+ I also need to define an ng-model).

```
<div class="form-group">
    	<div ng-repeat="fn in fileNames">
            <div class="form-group" ng-attr-id="{{fn + 'Row'}}">
                <div class="row">
                    <div class="col-sm-6">
                        <input type="file"
                               class="form-control"
                               ng-model="$parent.fileModels[fn]"
                               cam-variable-name="{{fn}}"
                               cam-variable-type="File"
                               cam-max-filesize="10000000"/>
                    </div>
                    <div class="col-sm-4">
                        <button type="button" class="btn btn-default" ng-click="removeFile(fn)">
                            <span class="glyphicon glyphicon-minus-sign"></span>
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>

```

Failed validation (camVariableType error + required error)

```
<input type="file" class="form-control ng-dirty ng-invalid ng-invalid-cam-variable-type" ng-model="$parent.fileModels[fn]" cam-variable-name="DYN_FILE_1460965019162" cam-variable-type="File" cam-max-filesize="10000000">

```

I think all these exceptions is related to the fact that some of the behavior of the directives are not defined in the controllers [but in custom external code](https://github.com/camunda/camunda-bpm-sdk-js/blob/c87efde19cfe67616e88e0994359f38398e02611/lib/angularjs/forms/camunda-form-angular.js);

```
 // next perform auto-scope binding for all fields which do not have custom bindings
    function autoBind(key, el) {
      var element = $(el);
      if(!element.attr('ng-model')) {
        var camVarName = element.attr(constants.DIRECTIVE_CAM_VARIABLE_NAME);
        if(!!camVarName) {
          element.attr('ng-model', camVarName);
        }
      }
    }

```

I could be mistaken so if there is a way to fix this I would be more than happy to hear it.

Kr,  
Nico

---

<div class="post-metadata">

**Author:** ![sebastian.stamm](https://forum.camunda.io/user_avatar/forum.camunda.io/sebastian.stamm/32/31_2.png) [@sebastian.stamm](https://forum.camunda.io/u/sebastian.stamm)\
**Post date:** [April 18, 2016, 8:38am UTC](https://forum.camunda.io/t/ad-hoc-camunda-element-creation-issue/437/2 "2016-04-18T08:38:58Z")

</div>

Hi Nico,

the `cam-variable-name` and `cam-variable-type` directives are only applied once when the form is rendered initially. That means you cannot add elements with `cam-variable-name` attribute dynamically.

To get around this restriction, you can have a look at the [Submitting Additional Variables](https://docs.camunda.org/manual/7.4/reference/embedded-forms/javascript/lifecycle/#submitting-additional-variables) and [Upload Large Files](https://docs.camunda.org/manual/7.4/reference/embedded-forms/javascript/examples/#upload-large-files) examples.

Cheers  
Sebastian

---

<div class="post-metadata">

**Author:** ![nvanbelle](https://forum.camunda.io/user_avatar/forum.camunda.io/nvanbelle/32/3452_2.png) [@nvanbelle](https://forum.camunda.io/u/nvanbelle)\
**Post date:** [April 18, 2016, 8:55am UTC](https://forum.camunda.io/t/ad-hoc-camunda-element-creation-issue/437/3 "2016-04-18T08:55:37Z")

</div>

Thank you. This piece of code particularly looks like a solution. I will try it.

```
$scope.upload = function() {
            var formData = new FormData();
            formData.append('data', document.getElementById('fileUpload').files[0]);
            $http.post(Uri.appUri('engine://engine/:engine/process-instance/' + result.processInstanceId + '/variables/uploadedFile/data'), formData, {
                transformRequest: angular.identity,
                headers: {'Content-Type': undefined}
            });
          };

```

I found that the camVariableType error I got was due to the fact that I used File while I should have used Bytes + ng-required=‘false’ got me also rid of the required exception. But as you said, as all this code is not in the lifecycle of the camunda form, the form did not submit my variable - even when added to the variableManager.

---

<div class="post-metadata">

**Author:** ![nvanbelle](https://forum.camunda.io/user_avatar/forum.camunda.io/nvanbelle/32/3452_2.png) [@nvanbelle](https://forum.camunda.io/u/nvanbelle)\
**Post date:** [April 18, 2016, 9:24am UTC](https://forum.camunda.io/t/ad-hoc-camunda-element-creation-issue/437/4 "2016-04-18T09:24:05Z")

</div>

It works. Here is my code if someone would ever be interested

```
       $scope.uploadFile = function(fileName) {
            if (document.getElementById(fileName).files[0] && document.getElementById(fileName).files[0].size > 0) {
                var formData = new FormData();
                formData.append('data', document.getElementById(fileName).files[0]);
                formData.append('valueType', 'File');
                $http.post('/engine-rest/task/' + $scope.camForm.taskId + '/variables/'+ fileName +'/data', formData, {transformRequest: angular.identity, headers: {'Content-Type': undefined}});
            }
        };
```

---

<div class="post-metadata">

**Author:** ![system](https://forum.camunda.io/uploads/default/original/3X/5/8/58d50142eacca605d35cb1ef9e6b86ae85f49436.png) [@system](https://forum.camunda.io/u/system)\
**Post date:** [January 30, 2024, 10:34am UTC](https://forum.camunda.io/t/ad-hoc-camunda-element-creation-issue/437/5 "2024-01-30T10:34:38Z")

</div>

Closed on January 30, 2024, 10:34am UTC


