6

When I submit this Polymer form with document.getElementById("form").submit(); firstName and lastName are included in the POST-data, but the title-value from the paper-dropdown-menu not. What is missing ?

<form is="iron-form" id="form" method="post" action="/edit">
    <paper-dropdown-menu name="title" label="Title">
        <paper-menu class="dropdown-content">
            <template is="dom-repeat" items="{{titles}}" as="title">
                <paper-item value="{{item.id}}">{{title.name}}</paper-item>
            </template>
        </paper-menu>
    </paper-dropdown-menu>
    <paper-input name="firstName" label="First name"></paper-input>
    <paper-input name="lastName" label="Last name"></paper-input>
    <paper-button raised onclick="submitForm()">Save</paper-button>
</form>

Edit:

Here my complete working example, thanks a lot to @Brandon for his answer:

<form is="iron-form" id="form" method="post" action="/api/edit">
    <paper-dropdown-menu label="Title" selected-item="{{selectedItem}}" selected-item-label="{{selected}}">
        <paper-menu class="dropdown-content">
            <template is="dom-repeat" items="{{titles}}" as="title">
                <paper-item value="[[title.id]]">[[title.name]]</paper-item>
            </template>
        </paper-menu>
    </paper-dropdown-menu>
    <input is="iron-input" name="title" type="hidden" value$="[[selectedItem.value]]">
    <paper-input name="firstName" label="First name"></paper-input>
    <paper-input name="lastName" label="Last name"></paper-input>
    <paper-button raised onclick="document.getElementById('form').submit()">Save</paper-button>
</form>
2
  • Might be because paper-dropdown-menu doesn't implement Polymer.IronFormElementBehavior?
    – Justin XL
    Commented Aug 10, 2015 at 23:14
  • @Justin XL, by chance do you know if there is a different element which can be used to get a <select>-style element inside a form ?
    – yglodt
    Commented Aug 11, 2015 at 5:49

1 Answer 1

8

This may solve your problem. Create a hidden input element and assign the selected item to the hidden element's value. This gives you the added benefit of the iron-input validators for multi-select for future forms.

<form is="iron-form" id="form" method="post" action="/edit">
    <paper-dropdown-menu label="Title" selected-item-label="{{selected}}">
        <paper-menu class="dropdown-content">
            <template is="dom-repeat" items="{{titles}}" as="title">
                <paper-item>{{title.name}}</paper-item>
            </template>
        </paper-menu>
    </paper-dropdown-menu>
    <input is="iron-input" name="title" type="hidden" value$="[[selected]]">
    <paper-input name="firstName" label="First name"></paper-input>
    <paper-input name="lastName" label="Last name"></paper-input>
    <paper-button raised onclick="submitForm()">Save</paper-button>
</form>
8
  • 1
    Thanks for your answer. I will try it out. Generally I start doubting in the quality of Polymer. Why does it need such a botch to just create a form with a dropdown ?
    – yglodt
    Commented Aug 12, 2015 at 8:23
  • You could just use the standard HTML select. But if you want something that looks better than then average form, sometimes it takes a little extra.
    – Brandon
    Commented Aug 12, 2015 at 12:38
  • Thank you. Polymer is great but there are so many nuances that its documentation does not capture. This is a lifesaver.
    – user568109
    Commented Aug 22, 2015 at 23:46
  • 2
    Not a problem. using your original paper-item element (with the value attribute) change: <paper-dropdown-menu label="Title" selected-item="{{selectedItem}} selected-item-label="{{selected}}"> <input is="iron-input" name="title" type="hidden" value$="[[selectedItem.value]]"> Working sample here: jsbin.com/luvuvo/edit?html,output
    – Brandon
    Commented Aug 23, 2015 at 16:05
  • 1
    Thanks a lot @Brandon, you rock.
    – yglodt
    Commented Aug 24, 2015 at 19:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.