Attempting to make use of a dynamic form in AngularJS, the code snippet below has been utilized:
<dynamic-form
template="formTemplate"
ng-model="formData"
ng-submit="processForm()">
</dynamic-form>
The controller script includes the following lines:
$scope.formData = {}; // A JavaScript object is needed to store the form's models.
$scope.formTemplate = [];
The 'onsuccess' function looks like this: console.log('User registration form :'+ response); // The form-id will be transformed to a string // var form-id = response.form-id; var result = toArray(response); $scope.formTemplate = result;
}
function toArray(obj) {
var result = [];
var model = [];var type = [];var label = [];
for (var prop in obj) {
var value = obj[prop];
angular.forEach(obj.fields, function(val,key){
if(val.field_type != ''){type.push(val.field_type);}
else{type.push(0);}
model.push(key);
});
}
for(var j=0;j<model.length;j++){
if(type[j] === 'textfield' || type[j] === 'password'){
type[j] = 'text';
}
else if(type[j] === 'email'){
type[j] = 'email';
}
else{
type[j] = type[j];
}
console.log("result.. " + model[j] + "......"+ type[j]);
result.push(
{
"type": type[j],
"label": model[j],
"model": model[j]
}
);
}
console.log("Result..." + result);
return result;
}
This dynamic form library is being used: https://github.com/danhunsaker/angular-dynamic-forms
However, while the given example in the library works fine, the scenario above does not. Any ideas on what might be causing the issue? Is it related to model evaluation?
The sample response provided by me is as follows:
var response = {
"form-id":"user_register_form",
"fields":{
"name":{
"field_type":"textfield",
"description":""
},
"mail":{
"field_type":"textfield",
"description":""
},
"field_first_name":{
"field_type":"textfield",
"description":""
},
"field_gender":{
"field_type":"radio",
"options":{
"male":"Male",
"female":"Female"
},
"description":""
}
}
}