psaia / react-serial-forms

A lightweight and extendable SSR-friendly form library (for React).
167 stars 18 forks source link

input values starting with number:value result in wrong values #3

Closed carlosrberto closed 9 years ago

carlosrberto commented 9 years ago

Hi, i love this project, but there is an issue: input values starting with number:value result in wrong values examples:

10:20 => 10
11:aa => 11

see the example of page: http://levinteractive.github.io/react-serial-forms/examples/demo.html

carlosrberto commented 9 years ago

the problem is this float conversion:

...
} else if (/^[0-9.]+/.test(json.value)) {
    val = parseFloat(json.value);
...

use the constant NUMBER_LIKE to fix the regex

...
} else if (NUMBER_LIKE.test(json.value)) {
    val = parseFloat(json.value);
...

https://github.com/LevInteractive/react-serial-forms/blob/master/src/FormBase.js#L157-L158

psaia commented 9 years ago

Definitely a bug. Just fixed it with https://github.com/LevInteractive/react-serial-forms/commit/dc97cb84c676bbe34ec0652b33311e39788b1049

Thank you!

psaia commented 9 years ago

Just published 1.1.0 which has this fix. I've also updated the demo.

carlosrberto commented 9 years ago

Thank you!

sekirocc commented 9 years ago

What if I actually want to post a string value to backend? If the user input is NUMBER_LIKE, it will be parsed to number value by mutateValues function. But what the server really want is a string value in json? example: {"keywords": 1234} vs {"keywords": "1234"} they are different in server view.

psaia commented 9 years ago

I believe the correct answer should be that the input type should dictate this.

I'll reopen this issue and add some tests to back it. I should be able to get to it this evening or tomorrow. Thanks!

psaia commented 9 years ago

Now v1.1.3 holds a fix for this. Please see this: https://github.com/LevInteractive/react-serial-forms#numbers