jonjamz / blaze-forms

Dead easy reactive forms with validation (Meteor).
https://atmospherejs.com/templates/forms
MIT License
113 stars 11 forks source link

The 'changed' argument of the action function does not contain fields that had no initial value #13

Closed steph643 closed 9 years ago

steph643 commented 9 years ago

This seems to be a design choice (cf. this comment). However, a standard use case is that you have an optional field in your collection, initially undefined. In the update form, you enter a value for this field. Then you have to update the collection with this new value. If the field is not present in changed, you still have to scan this, and changed is useless...

jonjamz commented 9 years ago

Ah--good catch. That totally makes sense.

jonjamz commented 9 years ago

This should solve it.

steph643 commented 9 years ago

Fix works great.

steph643 commented 9 years ago

Remaining issue: changed still does not work if the form was initialized with no data argument (it works well if there was an empty data argument).

    {{! Init the form with no data argument}}
    {{#myFormBlock schema=schema action=action}}
        {{> myInput field='testField'}}
    {{/myFormBlock}}

In that case, if user enters something in the input, changed will be empty instead of containing the new value.

jonjamz commented 9 years ago

I did it this way on purpose because without initial data nothing will have "changed" and the fields with values would all be in this in the action function. That way you can use:

if (_.isEmpty(changed)) { 
  // insert with `this`... 
} else {
  // update with `changed`...
}

I'll try to make this easier by returning undefined or false for changed if there's no initial value, rather than simply an empty object.

steph643 commented 9 years ago

Ok, it makes sense. If explained in the doc, an empty object is fine (but I think undefined would be even better).