Skip to content

A way to create an empty list when no checkboxes are selected. #49

@jjfine

Description

@jjfine

Given the following unselected inputs:

<dl>
    <dt>Favorite food</dt>
    <dd><label><input type="checkbox" name="person.favFood[]" value="steak"> Steak</label></dd>
    <dd><label><input type="checkbox" name="person.favFood[]" value="pizza"> Pizza</label></dd>
    <dd><label><input type="checkbox" name="person.favFood[]" value="chicken"> Chicken</label></dd>
</dl>

How could form2js build the following JSON for me?

{ person: { favFood: [] } }

One solution would be to cast a set of square brackets to an empty list. Then you could initialize any list you want to be empty by adding a hidden input as follows:

<dl>
    <dt>Favorite food</dt>
       <input type="hidden" name="person.favFood" value="[]" />
    <dd><label><input type="checkbox" name="person.favFood[]" value="steak"> Steak</label></dd>
    <dd><label><input type="checkbox" name="person.favFood[]" value="pizza"> Pizza</label></dd>
    <dd><label><input type="checkbox" name="person.favFood[]" value="chicken"> Chicken</label></dd>
</dl>

Any other ideas?

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