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

Need a way to reset values of form fields #208

Closed
hwillson opened this issue Dec 12, 2016 · 10 comments
Closed

Need a way to reset values of form fields #208

hwillson opened this issue Dec 12, 2016 · 10 comments

Comments

@hwillson
Copy link
Contributor

Migrating over from meteor/meteor#2431 (has a pretty large number of upvotes).


(originally reported by @avital)

Say an app has an input field whose value is computed reactively:

<input value={{foo}} />

A user changes the value in the field directly in the browser (so the underlying value computed by {{foo}} hasn't changed). How does one implement a 'cancel' button that reverts the input fields back to their original value?

Since Deps and Blaze treat setting a reactive variable to the same value as a no-op, something like Session.set("foo", Session.get("foo")); won't do anything.

A (pretty bad) workaround for now is something like:

var original = Session.get("foo");
Session.set("foo", original + "--change-it-to-anything-but-the-original-value");
Deps.flush();
Session.set("foo", original);

Maybe we should have an explicit function for "reset form fields under a given DOM element"?

@sbrichardson
Copy link

sbrichardson commented Dec 13, 2016 via email

@nosizejosh
Copy link

I am having a similar issue, lucky to have followed the trail here. please does anyone have a workaround or solution to this?

@nosizejosh
Copy link

adding {{#autoForm collection="Activities" id="insertActivityForm" type="insert" doc=defaultValues}} in my case for a form {{> afFieldInput name='orderPrice' readonly=true value=defaultValues}} as suggested by aldeed from this here seems to have solved my problem.

@mitar
Copy link
Collaborator

mitar commented Jun 11, 2017

The solution was already mentioned above. You can also check some similar examples in Blaze Components guide. You can implement something similar in pure Blaze as well.

@msavin
Copy link

msavin commented Jul 8, 2018

Bump for this 👍

I notice it only happens when you are trying to empty an input and/or set a false-y value on it. Maybe that is causing a re-run to fail somewhere?

@msavin
Copy link

msavin commented Jul 8, 2018

I managed to get this working by adding Tracker to the data source underneath the form. It checks over each form element 50ms after the data sources changes, which has generally been enough for the templates to re-render. Just in case the elements are not ready, it would try again after 500ms.

// Sample from Meteor Candy

Tracker.autorun(function () {
	var formInputs = form.inputs.get();

	window.setTimeout(function () {
		formInputs.forEach(function (formInput) {
			var input = "MeteorCandy_form_" + formInput.id;
			var element = document.getElementById(input);

			var runPatch = function () {
				if (formInput.type === "boolean") {
					if (formInput.value === true) {
						element.checked = true;	
					} else {
						element.checked = false;
					}
				} else {
					element.value = formInput.value;
				}
			}
			
			if (element) {
				runPatch()
			} else {
				window.setTimeout(function () {
					console.log("Meteor Candy: ooh, this took unusually long to patch");
					if (element) {
						runPatch()
					}
				}, 500)
			}
		})	
	}, 50)
})

@laddi
Copy link

laddi commented Oct 30, 2018

I have to say, this is one of the many reasons people are moving away from Blaze. This handling is plain stupid, why on earth would the rendering engine remove the value attribute from the element and not simply find another way to handle this? Form reset is a fundamental action in HTML, having to create a hack workaround to get that to work correctly in Blaze is a dealbreaker...

@msavin
Copy link

msavin commented Oct 30, 2018

Meh, a hack here and there is worth the ease the Blaze provides - but I get you 100%

@laddi
Copy link

laddi commented Oct 30, 2018

Blaze has many excellent qualities, I agree. But sorry for the harshness of words, I'm just a bit miffed because I'm running into this very problem with complex forms and I don't find it particularly appealing to create workarounds... 😛

@filipenevola
Copy link
Collaborator

I'm closing this issue because it's too old.

If you think this issue is still relevant please open a new one.

Why? We need to pay attention to all the open items so we should keep opened only items where people are involved.

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

7 participants