Coder Social home page Coder Social logo

Add typing to form fields about form2js HOT 8 OPEN

awvalenti avatar awvalenti commented on June 14, 2024
Add typing to form fields

from form2js.

Comments (8)

maxatwork avatar maxatwork commented on June 14, 2024

Hello,

Great idea. I think we should use:

  • 'type' attribute (if it's 'number', 'date' etc) - for simplicity
  • 'data-type' if 'type' attribute is absent (like in selects) or
    value type cannot be figured out of 'type' attribute (like in hiddens,
    radios, checkboxes).

This conforms HTML5 standard and most browsers (IE6+ too) can use it
(via getAttribute() or getAttributeNode() methods).

from form2js.

awvalenti avatar awvalenti commented on June 14, 2024

"data-type" seems nice! I read some texts about adding custom attributes to HTML elements and I started using them on my framework project (on a different context). Using data-* is working fine for me. It doesn't interfere with existing attributes and should be officially supported in the near future.

I believe using always "data-type" (instead of sometimes "type") would be the best choice. type="number" renders differently on Firefox and Chrome. On Firefox (9, at least), it's the same as type="text". On Chrome, it displays up/down arrows so you can increment/decrement the number - not necessarily what the developer wants. The developer might not want to add HTML5 features to his/her site, and this would force him/her to do so.

It seems to me that using only "data-type" would be simpler, and also more consistent among all input types. The developer would still have the option of adding type="number" or "date" if he/she wants to, and this would not interfere on form2js.

What do you think?

from form2js.

maxatwork avatar maxatwork commented on June 14, 2024

I think "type" attribute should be considered also. We should figure data type from markup if we can, but probably, "data-type" attribute should have higher precendence than "type" (for more consistent interface).

from form2js.

awvalenti avatar awvalenti commented on June 14, 2024

I agree with you. If data-type has higher precedence, the interface will surely be consistent enough :).

I thought about the type="date" thing you mentioned. The Date type exists only on JS, not on JSON. So, if one is using form2js to convert forms to JSON (instead of pure JavaScript objects), he/she might want to use some custom function to convert from date to string. So, maybe it would be nice also if it's made possible to do something like data-type="customBla" or something, which would then call the function customBla() to convert from the field string value to whatever the user likes. I know it's already possible to do something like that by passing arguments when calling the form2js function, but maybe it would be nicer if one could express that directly on the input element.

Maybe some convetion would make it fine, like that "custom" thing. This one doesn't seem quite good, but anyway it's just an example.

What do you think?

from form2js.

maxatwork avatar maxatwork commented on June 14, 2024

Great idea! I think we should also add a 'custom attribute' parameter
and check for all elements with such attribute on deserialization.

from form2js.

marceloverdijk avatar marceloverdijk commented on June 14, 2024

I bumped on this issue as I was wondering if form2js would support with data types.

I wonder what the status is of this issue and implementing it.

One thing I would to add is also the discussion of number formatting.
E.g I develop a Dutch site and there users will enter 12,95 for 12 euros and 95 cents. Note the , as decimal separator instead of . .

from form2js.

xeoshow avatar xeoshow commented on June 14, 2024

Also would like to know the status of this issue since it is quite important for real production env ...
Thanks!

from form2js.

sukazavr avatar sukazavr commented on June 14, 2024

My solution:

            switch (value)
            {
                case 'null': value = null; break;
                case 'true': value = true; break;
                case 'false': value = false;
            }

            if (!isNaN(parseFloat(value)) && isFinite(value)) value = parseFloat(value);

Insert this code after line 93.

Realizes types of null, true, false, integer and float.

from form2js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.