caolan / caolan/forms

Trying to unravel the mystery of field binding

Open
#144 1 comment 0 reactions 0 assignees View on GitHub
Needs More Info
Dominant language
JavaScript
Stars
1k
Forks
161
PR merge metrics
No merged PRs in 30d

Description

I'm dynamically creating a form based on info loaded from a json file. I've tried everything I can think of to get a value loaded from the json to display in a widget but nothing I've tried works.
The documentation regarding binding is (in my opinion) a bit opaque.
(forgive me if binding is not the right functionality - it just seemed to be what I was looking for)

This is the last obstacle I have to overcome before using forms in production.
Could someone show me the error of my ways? Thanks in advance.

To summarize: As I build the form, I have a value that I want to insert into the form input element that will be visible when the page is rendered.

```
/* configObject is an object parsed from json I load from a file

each itemElement below contains:
{
"value": ,
"type":
,
"length":
}

i.e.
fieldName1: {
"value": "contentOfFieldName1",
"type": "string",
"length": 12
}

*/

_.each(configObject, function (itemElement, itemIndex) {
if (['integer', 'float'].indexOf(itemElement['type']) >=0) {
fieldType = 'number'
} else {
fieldType = itemElement['type'];
}
/*
At this point, itemElement.value = the value I want to insert as the visible value of the form field.
How do I do this?
I've tried newField.bind(itemElement.value);
*/
var newField = fields[fieldType]({required: true});

//newField.bind(itemElement.value); // no error but nothing happens

formObject[itemIndex] = newField;

//formObject[itemIndex].bind(itemElement.value); // no error but nothing happens
//formObject.bind({itemIndex: itemElement.value}); // error: formObject has no method 'bind' });
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.