Giter Club home page Giter Club logo

Comments (4)

sdetweil avatar sdetweil commented on May 28, 2024

on the playground onChange, I added " in front of function, and then went to the end of the line and did delete, and then repeated end of line and repeat til all the function was on one line, then added "

and now the form passes validation

from jsonform.

sdetweil avatar sdetweil commented on May 28, 2024

in my app where I need to pass form resident onxxx functions to the web page, i collapse all to a single line, and use escape for any embedded "

like this

"onChange": "(evt,node)=>{let value=$(evt.target).val();let p=$(evt.target).attr('name').split('[');let n=p[0];let i=parseInt(p[1]);$(\"[value*='\"+n+\"']\").closest('.tab-pane').find('.tab-content').find(\"[data-idx='\"+i+\"'] >div >input \").val(value).trigger('change')}"

the whole form is converted to text (from object) using JSON.stringify, BUT you have to add handlers for the conversions, because stringify/parse don't handle functions by default

this is my form parser in the browser web page

function parseData(data) {
    return JSON.parse(
      data,

      function (key, value) {
        if (typeof value === "string") {
          // only handle specified fields in jsonform
          switch (key) {
            case "onChange":
            case "onClick":
            case "onKeyUp":
              // get the function from the string
              // parens mean don't EXECUTE the function, just get its value
              value = eval("(" + value + ")");
              break;
          }
        }
        return value;
      }
    );
  }

my app uses socketio to send the all text content of the form to the web page from the server, and then the web page parses it (using function above) to an object that is then used to generate the form into the DOM like normal.

all this cause the function (onKey, OnChange...) aren't valid in JSON (as you've mentioned) and aren't executable in text form..(which is valid in JSON)...
and my 'form' is generated from some other code, so it can change frequently

// config socket events
  activesocket.on("json", function (incoming_json) {
    let data = parseData(incoming_json.slice(1, -1));
.
.
.
.
      // replace any form from last connection
      $("#result").html('<form id="result-form" class="form-vertical"></form>');
      // insert the new form
      $("#result-form").jsonForm(data);

here are the two JSON utility functions on the server side to process for embedded functions in the form

//
// used by JSON.stringify
//
function tohandler(key, value) {
  if (typeof value === "function") {
    return value + ""; // implicitly `toString` it
  }
  return value;
}
//
// used by JSON.parse
//
function fromhandler(key, value) {
  if (
    value &&
    typeof value == "string" &&
    (value.startsWith("(") || value.startsWith("function(")) &&
    value.endsWith("}")
  ) {
    return eval("(" + value + ")");
  }
  return value;
}

from jsonform.

plaroche-po avatar plaroche-po commented on May 28, 2024

Thank you for your answer. I can't try today but i will test this method tomorrow.

from jsonform.

plaroche-po avatar plaroche-po commented on May 28, 2024

With some adaptations for my case, this works great !
Thank you again.

from jsonform.

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.