formly-js / angular-formly

JavaScript powered forms for AngularJS
http://docs.angular-formly.com
MIT License
2.23k stars 405 forks source link

Key 0 quirks #658

Closed okelepko closed 8 years ago

okelepko commented 8 years ago

Add a field without a key at index 0 Observe defaultValue is not applied to it Add a field with key 0 Observe that it is bound to model[1], not model[0]

http://jsbin.com/paboqinage/1/edit?html,js,output

okelepko commented 8 years ago

http://jsbin.com/pesupobona/1/edit?html,js,output <- with issue number.

BarryThePenguin commented 8 years ago

Additionally if you have a field config like

return [{
    key: 1,
    type: 'input',
    defaultValue: 'Hello!',
  },
  {
    key: 0,
    type: 'input',
  }];

Both fields get updated, likewise if you change 0 to false

tuxtina commented 8 years ago

No key: Default values get put in the model. If no key is specified, there is nowhere to put the default value. The parseSet() function, which takes care of setting values in the model, will bail out instantly.

I suppose this is the correct behavior, though it might make sense to add a check that warns if defaultValue is used in a field that has no key?

Numbers as keys: Looking at parseSet()/parseGet(), the intended behavior seems to be that numeric keys mean array indexes. However, the same check that correctly exits parseSet() for the non-existing key, will also exit the function if a 0 key is used.

This check should probably be modified to permit 0 keys. I'll make a pull request for that...

BarryThePenguin commented 8 years ago

I'm guessing this was closed by #667