Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bind data for nested fields on dynamicals forms #172

Closed
Raphael-johanne opened this issue Feb 16, 2016 · 3 comments
Closed

Bind data for nested fields on dynamicals forms #172

Raphael-johanne opened this issue Feb 16, 2016 · 3 comments

Comments

@Raphael-johanne
Copy link

Hi,

I must create a form with nested fields, it works when showing the form and save data to MongoDb, but when i call bind method on form to edit it, only simple fields are binded (as title, code, ...) not the nested.

I don't know if it's a bug or my bad utilisation of 'forms' tool, so, let me share with you some parts of my code.

`
/**

  • complementary function to add field to form
    */
    complementaryFieldsFunction['selectFields'] = function(data){

    var formFields = [];

    if (typeof data !== 'undefined' && data !== null) {

     var values = {name:'values', content:{}};
    
     data.values.forEach(function(item, index){
    
         var keys = Object.keys(item);
    
         keys.forEach(function(item2, index) {
    
             var value = {
                code  : fields.string({label: "Code value", value: item[item2].code}),
                value : fields.string({label: "Value", value: item[item2].value })
             }
             values.content[index] = value;
    
         });
     });
     formFields.push(values);
    

    } else {
    formFields.push(
    {
    name : "values",
    content : {
    0: {
    code: fields.string({label:"Code value"}),
    value: fields.string({label:"Value"})
    }
    }
    }
    );

    }
    return formFields;
    }

module.exports.getEdit = function(data, type) {

 var formFields = {
     code   : fields.string({required : true, label : "Code"}),
     title  : fields.string({required : true, label : "Title"}),    
     type   : fields.string({widget: forms.widgets.hidden(), value : type})
 };

 if (typeof complementaryFieldsFunction[type+"Fields"]  === 'function') {
     complementaryFieldsFunction[type+"Fields"](data).forEach(function(item, index){
         formFields[item.name] = item.content;
     })
 }

 var form = forms.create(formFields);

 if (typeof data !== 'undefined' && data !== null) {
     form = form.bind(data);
 }

 return form;

};
`
The idea is :

Create a form witch contains fields depending parameters ( ... function(data, type) { ...).

HTML structure of the form :

`

Code
Title
Code value
Value

`

When I call " form = form.bind(data); " with data =

{
"code": "add",
"title": "teqstafsdfsdf",
"type": "select",
"_id": "56c379ab78fa578434cc861e",
"__v": 0,
"mdate": "2016-02-16T19:34:03.952Z",
"cdate": "2016-02-16T19:34:03.951Z",
"values": [
{
"0": {
"code": "b",
"value": "vv"
}
}
]
}

'code', 'title' and 'type' fields are correctly binded (I show the value of each one in the inputs tag) but the field 'values' is empty.

I saw on your git issues 'fields.object( ...' for nested element but haven't found documentation about it.

Do you have any Idea about my problem ?

Thanks in advance!

Raphael

@ljharb
Copy link
Collaborator

ljharb commented Feb 18, 2016

Basically, instead of an object literal being pushed into formFields, make a fields.object instead (so that it's a fully fleshed out form field).

You're right that this needs documentation - I can't even locate any tests or examples covering this behavior.

@Raphael-johanne
Copy link
Author

Hi,

Thank you for your answer !

I finally found MY mystake, it comes from my mongoose schema

var mongoose = require('mongoose');
var attributeSchema = new mongoose.Schema({
code : { type : String },
title : { type : String },
type : { type : String },
- values : { type : Array , "default" : [] },
+ values : { type : Object , "default" : {} },
cdate : { type : Date, default : Date.now },
mdate : { type : Date, default : Date.now }
});

mongoose.model('attribute', attributeSchema, 'attribute');

Grrrr ... :)

Thank you !

Raphael

@ljharb
Copy link
Collaborator

ljharb commented Feb 24, 2016

Awesome, glad to hear it!

@ljharb ljharb closed this as completed Feb 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants