Skip to content

Add ability to fill in checkboxes based on their value #4

Description

@lukaswojnar

Hi,

It would be perfect if Formlet is also able to save checkboxes state based solely on their value. Unfortunately Formlet is not able to select them.

Example Form (http://foundation.zurb.com/develop/download.html#customizeFoundation):

<input type="checkbox" name="scss_components[]" value="foundation/components/grid" checked="checked">
<input type="checkbox" name="scss_components[]" value="foundation/components/block-grid">

I have altered the saved bookmarklet so it is now able to select those checkboxes:

endsWith: function(str, suffix) {
  return str.indexOf(suffix, str.length - suffix.length) !== -1;
},
getElement: function(form, name, index, value) {
    var element;
    if (value && methods.endsWith(name, "[]")) {
      element = form.querySelector('input[name="'+name+'"][value="'+value+'"]');
    }
    else {
      element = form[name][index] || form[name];
    }
    return element;
},

Saved data now look like this:

"elements": [
        ["input-name[]", null, "input-value", "setChecked", true],
        ["scss_components[]", null, "foundation\\/components\\/grid", "setChecked", true],
        ["basic-input", null, null, "setValue", "Hello!"],
]

The code needs some polishing, but it works.

It would be great if you could build an enhancement around it and include it in the future release of Formlet.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions