11

I am trying to use paper-button with type attribute set to submit (as one would do with button element) to submit the enclosing form, but for some reason it is unable to submit the form. Is this a bug or feature?

How to make paper-button submit the form?

PS: I am in dart land (not js).

adarshaj
  • 1,224
  • 1
  • 11
  • 24

4 Answers4

7

As noticed by Gunter, you can create a custom element which extends some of native element with your desired semantics.

I encountered with your issue too and I've created simple element which gives ability to submit to paper-button

<polymer-element name="paper-button-submit" extends="button" noscript>
  <template>
    <style>
      :host {
        border: 0;
        background: transparent;
        padding: 0;
        font-size: inherit;
      }
    </style>
    <paper-button>
      <content></content>
    </paper-button>
  </template>
</polymer-element>

Then you can write this

<button type="submit" is="paper-button-submit">Add</button>

And will get a button with paper-like look

just-boris
  • 9,468
  • 5
  • 48
  • 84
  • I use polymer on Javascript, but I believe that it can work in Dart too. – just-boris Nov 16 '14 at 00:15
  • This works fine in Dart, and doesn't require any supporting dart code. I chose to call the component 'paper-native-button', because it doesn't actually define anything re. submit - that happens at point of use (which is more powerful). This seems a pretty elegant solution. – hawkett Feb 23 '15 at 03:24
  • Actually, I found I did need to add some supporting script to pass through the button attributes like 'raised'. Gist with changes/additions: https://gist.github.com/hawkett/49146a43d8595aa6c468 - 3 things to note - remove the 'noscript' attribute (http://mobilemancer.com/2015/01/06/rookie-mistakes-developing-web-components-in-polymer/), made the font inheritance more complete and added script to support the 'raised' attribute. – hawkett Feb 23 '15 at 06:45
  • I would love to pass attributes like `disabled`, `raised`, and `z` to the paper button. without the help of dart. If anyone gets there before me, please share! – morgs32 Feb 25 '15 at 00:23
7

You can achieve the form submit by placing a native button inside the paper-button element:

<paper-button>
  <button>Sign Up</button>
</paper-button>

Then use this following CSS to hide the native button while ensuring it's hitzone fills the entire paper-button element:

<style shim-shadowdom>
  paper-button {
    padding:0;
  }
  paper-button::shadow .button-content {
    padding:0;
  }
  paper-button button {
    padding:1em;
    background-color: transparent;
    border-color: transparent;
  }
  paper-button button::-moz-focus-inner {
    border: 0;
  }
</style>
Derrick
  • 349
  • 4
  • 11
6

There was already a discussion about using Polymer elements containing form elements within a form in the Polymer Google group and as far as I remember I answered a similar question here on SO (I will do some research afterwards).

1) You can extend an input element

<polymer-element name="my-element" extends="input">
   ...
</polymer-element>

and use it like

<input is="my-element">

2) You can do the form processing in custom code
(read the values from the form elements and create an AJAX call to send the data to the server)

3) Create a custom form element (extends the 2nd)
which does the form processing and AJAX call

The 1st option is not applicable to core-elments/paper-elements because the don't extend an <input> (or any other form element) but embed it. This applies to form input elements and also to the form submit button.

Some more or less related topics

What you can do if only the submit button is a Polymer element, is to invoke the click() method on an invisible (non-Polymer) submit button in the click handler of the <paper-button> for more details see
- Polymer: manually submitting a form

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Thanks for a comprehensive list of options, but unfortunately, this still doesn't answer my original question. I will wait for some more time to see if there's any workaround to get native form to be submitted via paper-button. – adarshaj Jul 21 '14 at 15:46
  • 1
    There is nothing planned so far, could be you have a very long time to wait. I guess you just have to do the submit processing 'manually' similar as shown in Seth's blog post. – Günter Zöchbauer Jul 21 '14 at 15:48
  • 3
    To be clear, a `paper-forms` project is already underway for Polymer JS. – Scott Miles Jul 21 '14 at 18:22
  • @ScottMiles Great to hear! Didn't saw it mentioned somewhere yet. – Günter Zöchbauer Jul 21 '14 at 20:05
  • 1
    Looks like the only way to easily submit these custom form fields is with [ajax-form](https://github.com/garstasio/ajax-form). This is what the Polymer team recommends as well. Disclaimer: I'm the author of ajax-form. – Ray Nicholus Nov 23 '14 at 01:04
  • @ScottMiles Source on that? – jonvuri Dec 10 '14 at 16:43
4

There is no need to create a custom element. According to the docs the following apporach is recommended:

<paper-button raised onclick="submitForm()">Submit</paper-button>
function submitForm() {
  document.getElementById('form').submit();
}

so you would just bind the onclick event to a function that manually submits your form.

UPDATE

Although the previous example from iron-form uses onclick event it is recommended to use on-tap over on-click:

Tip: Use on-tap rather than on-click for an event that fires consistently across both touch (mobile) and click (desktop) devices.

It is also a good idea to use Polymers own DOM API:

function submitForm(e) {
  Polymer.dom(e).localTarget.parentElement.submit();
}
Yan Foto
  • 10,850
  • 6
  • 57
  • 88