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

Checkbox value #1

Closed uovidiu closed 9 years ago

uovidiu commented 9 years ago

Hi, I'm using your script to render a dynamic form and store the data into database. The problem I have is with multiple choices, radio buttons. Do you think it make sense to add an option for checkbox values?

Using your example, something like this

{
  "schema": {
    "menu": {
      "type": "array",
      "title": "Options",
      "items": {
        "type": "string",
        "title": "Option",
        "enum": [
          "starter",
          "maincourse",
          "cheese",
          "dessert",
        "values": [
          "starter-val",
          "maincourse-val",
          "cheese-val",
          "dessert-val"
        ]
      },
      "default": [
        "starter",
        "maincourse"
      ]
  }
  },
  "form": [
    {
      "key": "menu",
      "type": "checkboxes",
      "titleMap": {
        "starter": "Starter would be great",
        "maincourse": "No way I'll skip the main course",
        "cheese": "Cheddar rules!",
        "dessert": "Thumbs up for a dessert"
      }
    }
  ]
}

Currently I did a small change in your script, replacing this

'<input type="checkbox" <% if (value) { %> checked="checked" <% } %> name="<%= name %>" value="1"' +

with this

'<input type="checkbox" <% if (value) { %> checked="checked" <% } %> name="<%= name %>" value="<%= title %>"' +
ulion commented 9 years ago

Hi @uovidiu,

I'm glad someone can get benefit from my enhanced code, and hopes you use the dev branch of my code which I'm working on it.

As your problem, I noticed that you add "values" in the schema? what's that for? why not put the values you want as the enum values?

for all radios/checkboxes/radiobuttons/checkboxbuttons type form elements, we can have 2 values for each option, one is title, which is for display, the other is value, which is the value in the json we got in onSubmit.

so, if you want "starter-val" in result json, then you just need put it in the enum of schema, and also use "starter-val" as the key of the titleMap of the form element definition. do I get the point?

uovidiu commented 9 years ago

I wasn't aware of that option, to set a value. I've checked the documentation and your examples from playground but no luck. And I'm not using dev branch either. I'm on a live project and I wanted to have a bug free branch. I will have a go with dev and see.

Can you give me an example on how to use values for checkboxes in a schema please? Maybe use the one I've used in my issue.

Thanks for your quick response.

ulion commented 9 years ago

Sorry, I still can not get what you want, you need explain more detail then I can help. the playground now is same code with my dev branch. The playground examples show more on how to use it in some way, specially for some enhanced feature.

uovidiu commented 9 years ago

You said "for all radios/checkboxes/radiobuttons/checkboxbuttons type form elements, we can have 2 values for each option, one is title, which is for display, the other is value, which is the value in the json we got in onSubmit." I don't know how to change the schema in order to achieve this. I want to submit a checkbox value, not just 1. And master branch doesn't support this, as you can see in that piece of code I changed. Value is hardcoded to '1'. Maybe dev branch allow this. I will check.

ulion commented 9 years ago

OK, so I got some point. so you are not use the result json in onSubmit, instead, you want the form submit itself to server directly?

to archive that, you need remove the enum in the schema, and use "options" property in the form element definition, check the 3rd question of http://ulion.github.io/jsonform/playground/bootstrap3/?example=fields-checkboxes which use options and this should works with checkboxes type question, or others, you need try and test.

2014-11-19 20:39 GMT+08:00 Ovidiu Ungureanu notifications@github.com:

You said "for all radios/checkboxes/radiobuttons/checkboxbuttons type form elements, we can have 2 values for each option, one is title, which is for display, the other is value, which is the value in the json we got in onSubmit." I don't know how to change the schema in order to achieve this. I want to submit a checkbox value, not just 1. And master branch doesn't support this, as you can see in that piece of code I changed. Value is hardcoded to '1'. Maybe dev branch allow this. I will check.

— Reply to this email directly or view it on GitHub https://github.com/ulion/jsonform/issues/1#issuecomment-63633810.

Ulion

uovidiu commented 9 years ago

I'm using this

var form = $('#preview');
form.on('submit', function (ev) {
        ev.preventDefault();

        $.ajax({
            type: "POST",
            url: "<?php echo SITE_MODULES;?>/forms/ajax_call.php",
            cache: false,
            data: {
                fields: $(this).serialize(),
                'form_id': '<?=$entry['id'];?>',
                'no_action': 'form_submit'
            },
            dataType: 'json',
            success: function(data){
//                  alert(data);
            }
        });

From your example I see that I can set values based on the array, I will change that in my schema and test. Thank you for your help.

ulion commented 9 years ago

Oh, you used $(this).serialize(). that's the reason. Since the jsonForm has its own way to get the json data from the form, in my newest dev branch, I exposed it so you can call $(this).jsonForm('values') to get the json structure without use onSubmit or onSubmitValid in the jsonform init options.