ulion / jsonform

Build forms from JSON Schema. Easily template-able. Compatible with Twitter Bootstrap out of the box.
http://ulion.github.io/jsonform/playground/
MIT License
49 stars 27 forks source link

jsonform is altering field values on render if they contain '[1]' in fieldsets #31

Open adam-clarey opened 8 years ago

adam-clarey commented 8 years ago

We have found that if you have a textfield within a fieldset and the default value is something like:

sometext[1]

When the form is rendered, line 2864:

this.value = applyArrayPath(this.value, this.arrayPath);

changes the value in the textfield to:

sometext[0]

This is clearly a bug. I don't see any reason why jsonform should be altering the values in fields.

The comment above that line is:

// Note applying the array path probably doesn't make any sense, // but some geek might want to have a label "foo[].bar[].baz", // with the [] replaced by the appropriate array path.

Which doesn't make much sense, he references changing the label but its actually changing the field value. It also only seems to be there for some odd edge-case.

As this is a bug that is hampering our project, should i submit a patch to remove the line I mentioned above? It seems to be a clear bug

ulion commented 8 years ago

Thank you for report. can you give a json schema to reproduce the problem you described?

adam-clarey commented 8 years ago

I've created this on the playground which mimics our problem. Our CMS IrisJS allows users to create complex entities via a UI so our forms are auto-generated quite a lot. In the below example, the default value in the textbox says 'letmein[0]" instead of "letmein[1]"

{
  "schema": {
    "author":{ 
      "type":"array",
      "title":"Users",
      "expandable":true,
      "default":[ { name:"letmein[1]"} ],
      "description":"",
      "items":{ 
        "type":"object",
        "properties": { 
          "name":{
            "type" : "text"
          }
        } 
      }
    }
  },
  "form": [{
    "key":"author",
    "type":"array",
    "items":[{ 
      "type":"fieldset",
        "items":[{ 
          "type":"text",
          "autocomplete":{ "source":"/entity-reference/user/username" },
          "key":"author[].name",
          "title":"Username/Email",
          "description":"" }] 

    } ] 

  }]
}
ulion commented 8 years ago

Just committed a workaround patch.