Subscribe on changes!

Failed setting prop "size" on <input>: value 0 is invalid.

avatar
Sep 7th 2022

Vue version

3.2.38

Link to minimal reproduction

https://codesandbox.io/s/peaceful-fermat-f8l7q1?file=/src/InputComponent.vue

Steps to reproduce

  1. Create a component for input.
  2. Add the option to specify :size via props.
  3. Use a component and don't specify size.

What is expected?

No warning.

What is actually happening?

Warning: image

System Info

No response

Any additional comments?

No response

avatar
Sep 8th 2022

see https://html.spec.whatwg.org/multipage/input.html#the-size-attribute

The size attribute, if specified, must have a value that is a valid non-negative integer greater than zero.

avatar
Sep 8th 2022

@edison1105 What does "if it is specified" have to do with it? Vue doesn't allow you to not specify a size. I want not to specify size. This attribute is optional! A warning in the console is bothering me. image

Behavior when: <input> and with: <input :size="undefined" It should be the same.

avatar
Sep 13th 2022

I see that the default value for the size is 20. Is it really necessary to set the default value props: {size: {default: 20}} and see it in the html, even if it is not specified when using the component? Can vue developers make it so that when trying to specify an undefined value, it is not set at all? So that there is no error and there is no size in the html?

avatar
Sep 13th 2022

@edison1105 I reopened because I think we have some room for optimization here:

https://github.com/vuejs/core/blob/8772a01a9280b1591e781e20741d32e2f9a836c8/packages/runtime-dom/src/modules/props.ts#L96-L107

This is the place that throws the warning. it does throw because it tries to do el.size = undefined, which will result in the size attribute being reflected as size="0", and an erroir being throws because undefined is not accepted as a value.

I think we can rewrite it like this?

if (needRemove) {
  el.removeAttribute(key)
}
else {
  try {
    el[key] = value
  } catch (e: any) {
    if (__DEV__) {
      warn(
        `Failed setting prop "${key}" on <${el.tagName.toLowerCase()}>: ` +
          `value ${value} is invalid.`,
        e
      )
    }
  }
}

However I'm not sure if the code as it is now was done to cover some other scenario.

Also, I think we need a fix here - needRemove is not bein set for cases where value null or undefined here.

https://github.com/vuejs/core/blob/8772a01a9280b1591e781e20741d32e2f9a836c8/packages/runtime-dom/src/modules/props.ts#L55-L69

Again, not sure of the side effects from the top of my head.

avatar
Sep 13th 2022

Looking at it again, this issue is even mentioned here in the comment in line 66.

But I guess it does not work like intended because the type check is a bit all over the place:

  • typeof input.max // => 'string'
  • typeof input.size // => 'number'
  • typeof img.size // => 'undefined'