Polymer - Iron Form



The <iron-form> is an HTML <form> element used to validate and submit any custom elements and native elements. Both the get and post methods are supported and ironajax element is used to submit the data to the action URL.

By default, the native button element submits the following form −

<form is = "iron-form" id = "form" method = "post" action = "/form/handler">
   <paper-input name = "password" label = "Password"></paper-input>
   <input name = "address">
   ...
</form>

Call the form's submit method explicitly, if you want to submit it from the custom element's click handler as shown in the following command.

<paper-button raised onclick = "submitForm()">Submit</paper-button>

function submitForm() {
   document.getElementById('form').submit();
}	

The following code shows the iron-form-presubmit event to which you can listen, if you want to customize the request sent to the server.

form.addEventListener('iron-form-presubmit', function() {
   this.request.method = 'put';
   this.request.params = someCustomParams;
});

Example

To implement the iron-form element, install the necessary elements and import them in your index.html file.

The following example demonstrates the use of iron-form element −

<!DOCTYPE html>
<html>
   <head>
      <title>iron-form</title>
      <base href = "https://polygit.org/polymer+1.5.0/components/">
      <script src = "webcomponentsjs/webcomponents-lite.min.js"></script>
      <link rel = "import" href = "polymer/polymer.html">
      <link rel = "import" href = "iron-form/iron-form.html">
      <link rel = "import" href = "paper-input/paper-input.html">
      <link rel = "import" href = "paper-button/paper-button.html">
      <link rel = "import" href = "paper-dropdown-menu/paper-dropdown-menu.html">
      <link rel = "import" href = "paper-menu/paper-menu.html">
      <link rel = "import" href = "paper-item/paper-item.html">
  
      <style>
         .paperbtn {
            background: #4682B4;
            color: white;
         }
         .paperinput{
            width: 25%;
         }
         .menu{
            width:25%;
         }
      </style>
   </head>
  
   <body>
      <form is = "iron-form" method = "get" action = "/" id = "basic">
         <paper-input class = "paperinput" name = "name" label = "Enter your name" required<
         </paper-input>
         <br>
         <input type = "checkbox" name = "vehicle" value = "bike"> I have a bike
         <br>
         <input type = "checkbox" name = "vehicle" value = "car"> I have a car
         <br>
     
         <paper-dropdown-menu class = "menu" label = "Icecream Flavours" name = "Flavours">
            <paper-menu class = "dropdown-content">
               <paper-item value = "vanilla">Vanilla</paper-item>
               <paper-item value = "strawberry">Strawberry</paper-item>
               <paper-item value = "caramel">Caramel</paper-item>
            </paper-menu>
         </paper-dropdown-menu><br>
       
         <paper-button class = "paperbtn" raised onclick = "_submit(event)">Submit</paper-button>
         <paper-button class = "paperbtn" raised onclick = "_reset(event)">Reset</paper-button>
         <h4>You entered the details:</h4>
         <div class = "output"></div>
      </form>
   
      <script>
         function _submit(event) {
            Polymer.dom(event).localTarget.parentElement.submit();
         }
         function _reset(event) {
            var form  =  Polymer.dom(event).localTarget.parentElement
            form.reset();
            form.querySelector('.output').innerHTML  =  '';
         }
         basic.addEventListener('iron-form-submit', function(event) {
            this.querySelector('.output').innerHTML  =  JSON.stringify(event.detail);
         });
      </script>
   </body>
</html>

Output

To run the application, navigate to your project directory and run the following command.

polymer serve

Now open the browser and navigate to http://127.0.0.1:8081/. Following will be the output.

Iron Form
polymer_elements.htm
Advertisements