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

don't set undefined input value #3430

Merged
merged 1 commit into from
Aug 20, 2019
Merged

don't set undefined input value #3430

merged 1 commit into from
Aug 20, 2019

Conversation

Rich-Harris
Copy link
Member

This fixes #1233. Instead of blindly setting input.value to its binding value, the value is only set if it's not undefined or null (or if the value was previously truthy).

This way, Firefox doesn't present a newly-created <input> as invalid when it hasn't had a value set yet (for this to work, the value must initially be undefined rather than "").

@codecov-io
Copy link

codecov-io commented Aug 19, 2019

Codecov Report

Merging #3430 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #3430   +/-   ##
=======================================
  Coverage   50.25%   50.25%           
=======================================
  Files           1        1           
  Lines         197      197           
=======================================
  Hits           99       99           
  Misses         98       98

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1bb0728...dcd9276. Read the comment docs.

@DmitryMyadzelets
Copy link

DmitryMyadzelets commented Nov 29, 2021

This merge is somehow broken in the current master (Svelte v3.44.2):

export function set_input_value(input, value) {
	input.value = value == null ? '' : value;
}

The fix was:

export function set_input_value(input, value) {
	if (value != null || input.value) {
		input.value = value;
	}
}

So the issues #4725 and #1233 take place again.

Update: The fix wasn't correct, btw. If the value is 0 or "" it wouldn't set input.value.

Update: Commits where the issue evolves: 081f7cd, d8fb0bb

I'm not aware why null is a special case for the input value. I experience this issue when the values are not defined but required when the <form> is loaded. It seem a common default state. I'd propose the next fix:

export function set_input_value(input, value) {
	if (value != null && value != undefined) {
		input.value = value;
	}
}

However, this breaks intentional assigning of undefined to the values. Then, we have to avoid setting input.value in case it's not defined in the initial state only (mounting?).

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

Successfully merging this pull request may close these issues.

Initial Data for Bound Required Input Fields is 'undefined'
3 participants