jonjamz / blaze-forms

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

'this' does not contain validated form values in action callback #10

Closed steph643 closed 9 years ago

steph643 commented 9 years ago

When using data= to setup initial form values, this of the action callback contains those initial values instead of the new validated values.

This issue does not occur when not using data=.

A workaround is to use the els argument to get the value from the DOM:

        return function(els, callbacks) {
            var fields = {};
            els.forEach(function(el)
                {
                var $el = $(el);
                var name = $el.attr('name');
                check(name, String);
                fields[name] = $el.val();
                });

            // Here 'this' contains the old values and 'fields' contains the new ones
            ...
        }
jonjamz commented 9 years ago

Thanks for the workaround. I will fix this ASAP.

jonjamz commented 9 years ago

Just want to confirm--does this happen when you've made changes to the form elements that passed validation?

steph643 commented 9 years ago

I cannot reproduce the issue! Please let me check.

steph643 commented 9 years ago

Got it: the issue occurs when I change the value programatically (using $element.val(newValue)). The issue does not occur when changing the value manually (normal case). My mistake is that I expected the package to behave the same way when changing values manually and programatically. Sorry for that.

steph643 commented 9 years ago

I will add a feature request for a function to change values programatically :-)

jonjamz commented 9 years ago

When you register an element, the validationEvent is what triggers the change. So you can probably pull it off just by triggering that event on the element with the ".reactive-element" class with jQuery after the update.

steph643 commented 9 years ago

Your suggestion works like a charm, thanks a lot!