Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checkbox value #1

Closed
uovidiu opened this issue Nov 18, 2014 · 7 comments
Closed

Checkbox value #1

uovidiu opened this issue Nov 18, 2014 · 7 comments

Comments

@uovidiu
Copy link

uovidiu commented Nov 18, 2014

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
Copy link
Owner

ulion commented Nov 18, 2014

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
Copy link
Author

uovidiu commented Nov 19, 2014

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
Copy link
Owner

ulion commented Nov 19, 2014

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
Copy link
Author

uovidiu commented Nov 19, 2014

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
Copy link
Owner

ulion commented Nov 19, 2014

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
#1 (comment).

Ulion

@uovidiu
Copy link
Author

uovidiu commented Nov 19, 2014

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
Copy link
Owner

ulion commented Nov 19, 2014

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.

@ulion ulion closed this as completed Jun 29, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants