![Ideal Forms logo](http://i.imgur.com/wg9htKk.png) The best forms just got better! Ideal Forms 3 is smaller, faster, and more extensible. **License:** GPL or MIT **Support:** IE9+ and all modern browsers **Website:** http://bit.ly/GFrp55 (not always in sync) **Demo only:** http://bit.ly/1ahZalu ### Features - On the spot validation - Fully adaptive (adapts to the container, no css media queries needed) - Keyboard support - Custom checkboxes, radios and file inputs - Custom seamless jQuery UI datepicker - Support for third party extensions - Localization ### Major changes since version 2 Ideal Forms 3 is **not** compatible with version 2. You can still find Ideal Forms 2 under [jq-idealforms-old](https://github.com/elclanrs/jq-idealforms-old), but support has been dropped. Here's what's new in version 3: - New architecture, more modularity - Custom markup - Extensions - Improved custom checkbox, radio and file inputs - Improved built-in theme - Switch to Stylus - Drop custom select menu, but check out [jquery.idealselect](https://github.com/elclanrs/jquery.idealselect) - Drop support for IE8 ### TODO - Image Preview Field ## Table of Contents - [Setup](#setup) - [Options](#options) - [Markup](#markup) - [Markup with Rules](#markup-with-rules) - [Custom Markup](#custom-markup) - [Adding Rules](#adding-rules) - [Custom Errors](#custom-errors) - [Built-in Rules](#built-in-rules) - [Methods](#methods) - [Built-in Extensions](#built-in-extensions) - [Dynamic Fields](#extension-dynamic-fields) - [Steps](#extension-steps) - [Custom Inputs](#extension-custom-inputs) - [Ajax](#extension-ajax) - [Datepicker](#extension-datepicker) - [Adaptive](#extension-adaptive) - [Custom Rules](#custom-rules) - [Custom Extensions](#custom-extensions) - [Localization](#localization) - [Themes](#themes) - [FAQ](#faq) - [Build & Share](#build--share) - [Update History](#update-history) ## Setup - Load latest [jQuery](http://code.jquery.com/jquery-2.0.3.min.js) library - Load `css/jquery.idealforms.css` stylesheet - Load `js/out/jquery.idealforms.min.js` plugin - To localize Ideal Forms load the corresponding file from `js/i18n`. See [Localization](#localization). - Place images in a folder and make sure the [path is correct](#why-the-icons-dont-show-up) - For better IE support, replace the opening `html` tag with: ```html ``` - Call Ideal Forms on your form: ```javascript $('form').idealforms({ options }); ``` [![TOC](http://i.imgur.com/RDbarNr.png)](#table-of-contents) ## Options ```javascript defaults = { field: '.field', error: '.error', iconHtml: '', iconClass: 'icon', invalidClass: 'invalid', validClass: 'valid', silentLoad: true, onValidate: $.noop, onSubmit: $.noop, rules: {}, errors: {} } ``` ### field The field container for [custom markup](#custom-markup). ### error The error container for custom markup. ### iconHtml The element to use as icon. Set to `false` to disable icons. ### iconClass The class for the icon element. ### invalidClass The class that will be added to invalid fields. ### validClass The class that will be added to valid fields. ### silentLoad Initialize the form silently, otherwise focus the first invalid input. ### onValidate(input, rule, valid) Callback that runs after an input attempts to validate. - **input:** The input being validated - **rule:** The rule that the input is validating. - **valid:** Boolean. Did it validate? ### onSubmit(invalid, event) Callback that runs when the form is submitted. - **invalid:** The number of invalid fields if any. - **event:** The submit event. Example: ```javascript $('form').idealforms({ onSubmit: function(invalid, e) { e.preventDefault(); if (invalid) { alert(invalid +' fields!'); } else { $.post('save.php', this.$form.serialize(), function(response) { // do something with response }, 'json'); } } }); ``` ### rules Field rules. See [Adding Rules](#adding-rules). ### errors User defined errors for added rules. See [Custom Errors](#custom-errors). [![TOC](http://i.imgur.com/RDbarNr.png)](#table-of-contents) ## Markup You can get started quickly using Ideal Forms' default markup: ```html
``` ### Markup with Rules You can build simple forms by adding the rules to the markup, using the `data-idealforms-rules` attribute: ```html ``` ### Custom Markup Ideal Forms 3 has been built from scratch with flexibility in mind. The markup is no longer tied to the plugin. If the default markup doesn't work for you, you can create your own markup. Ideal Forms will look for the following: - **A field:** A field must contain at least a label, an input and an error. - **A label:** The label to identify the field. - **An input or group of inputs:** Must be a single input or multiple related inputs such as checkboxes or radios. A field _cannot_ contain inputs with different `name` attributes; this is a limitation by design. - **An error container:** An element to hold the error. Then you have to tell Ideal Forms that you want to use custom markup. You can specify these options when initializing the plugin: ```javascript $('form').idealforms({ field: '.myfield', // selector error: '.myerror', // selector validClass: 'myvalid', // class name invalidClass: 'myinvalid' // class name }); ``` [![TOC](http://i.imgur.com/RDbarNr.png)](#table-of-contents) ## Adding Rules Pass an object to the `rules` option, where each key corresponds to a `name` attribute and each value is a string of rules assigned to that input. Always quote keys for consistency: ```javascript $('form').idealforms({ rules: { 'username': 'required username', 'password': 'required password', 'sex': 'minoption:1', 'hobbies[]': 'minoption:1 maxoption:2', 'options': 'select:default' } }); ``` You can also add rules after initializing the plugin: ```javascript $('form').idealforms('addRules', { 'comments': 'required minmax:50:200' }); ``` ### Custom Errors To display your own custom errors for the rules you added pass the names and a list of errors for each field: ```javascript $('form').idealforms({ rules: { ... }, errors: { 'username': { required: 'Please enter a username', username: 'This username is not valid, try again' }, 'password': { password: 'Please enter a secure password' } } }); ``` [![TOC](http://i.imgur.com/RDbarNr.png)](#table-of-contents) ## Built-in Rules A rule must be in this format `rule:param` where `rule` is the name of the `rule` and `param` is a rule parameter, for example `minmax:10:50` will use the `minmax` rule with two arguments, `10` and `50`. - **required:** The field is required. Only works with text inputs. - **digits:** Only digits. - **number:** Must be a number. - **username:** Must be between 4 and 32 characters long and start with a letter. You may use letters, numbers, underscores, and one dot. - **email:** Must be a valid email. - **pass:** Must be at least 6 characters long, and contain at least one number, one uppercase and one lowercase letter. - **strongpass:** Must be at least 8 characters long and contain at least one uppercase and one lowercase letter and one number or special character. - **phone:** Must be a valid US phone number. - **zip:** Must be a valid US zip code - **url:** Must be a valid URL. - **range:min:max:** Must be a number between `min` and `max`. Usually combined with `number` or `digits`. - **min:min:** Must be at least `min` characters long. - **max:max:** Must be no more that `max` characters long. - **minmax:min:max:** Must be between `min` and `max` characters long. - **minoption:min:** Must have at least `min` checkboxes or radios selected. - **maxoption:max:** Must have no more than `max` checkboxes or radios selected. - **select:default:** Make a select required, where `default` is the value of the default option. - **extension:ext:** Validates file inputs. You can have as many `ext` as you want. - **equalto:name:** Must be equal to another field where `name` is the name of the field. - **date:format:** Must a valid date in any format. The default is `mm/dd/yyyy` but you can pass any format with any separator, ie. `date:yyyy-mm-dd`. - **ajax:** See the built-in [Ajax Extension](#extension-ajax). [![TOC](http://i.imgur.com/RDbarNr.png)](#table-of-contents) ## Methods To call a method run `idealforms` on the form and pass the method and arguments: ```javascript` $('form').idealforms('method', arg1, arg2, ...); ``` Methods that have a colon like `is:valid` return something other than the instance, so they can't be chained. ### .idealforms('addRules', rules) See [Adding Rules](#adding-rules) ### .idealforms('get:invalid') Returns a jQuery collection of all invalid fields. Access the `length` to check how many fields are invalid. ### .idealforms('focusFirstInvalid') Focus the first invalid field. ### .idealforms('is:valid', name) If you pass a `name` it will check if that input is valid, otherwise it will check if all inputs are valid. ### .idealforms('reset', name) If you pass a `name` it will reset that single input, if you don't it will reset all inputs to zero. That means emptying all the values of text inputs, unchecking all checkboxes and radios, and reverting selects to their default option. [![TOC](http://i.imgur.com/RDbarNr.png)](#table-of-contents) ## Built-in Extensions Ideal Forms 3 has been re-designed to be able to extend the core easily. Read on [Custom Extensions](#custom-extensions) to learn more. Ideal Forms comes with a few built-in extensions. Extensions can be disabled with the `disabledExtensions` option by passing a space separated string of extensions. ```javascript $('form').idealforms({ disabledExtensions: 'dynamicFields steps customInputs ajax' }); ``` ### Extension: Dynamic Fields **Name:** `dynamicFields` Dynamic Fields extends core with the following methods: #### .idealforms('addFields', fields) Add fields to the form. - **fields:** And object where each key corresponds to the `name` attribute. The value of the object is another object that can contain any of the following options (* are required): ```javascript { type*: 'text:subtype | file | group:subtype | select', label*: 'Label', value: 'value', attrs: 'attr="value"', after: 'name', before: 'name', list: [ { value: 'value', text: 'text' } ... ], rules: 'rule rule rule' } ``` Text subtypes include all HMTL5 text types, such as `email`, `number`, etc... Group subtypes include `radio` and `checkbox`. If `before` or `after` are not set, the field will be added at the end of the form. **Example:** ```javascript $('form').idealforms('addFields', { 'fruits[]': { type: 'group:checkbox', label: 'Fruits', list: [ { value: 'apple', text: 'Apple', attrs: 'checked' }, { value: 'orange', text: 'Orange' }, { value: 'banana', text: 'Banana' } ], rules: 'minoption:1 maxoption:2' } }); ``` The HTML is generated according to built-in templates. If you're using your own custom markup you may need custom templates. Ideal Forms provides a simple templating system to easy the pain. These are the default templates that you may change in the options when calling the plugin. They are listed as real HTML for convenience but you must pass a string in the options (you can get the HTML from a script tag for example): ```html{@list} {/list}
``` ```javascript $('form').idealforms({ templates: { base: '...', text: '...', file: '...', textarea: '...', group: '...', select: '...' } }); ``` The templating rules are: - **{var}:** A variable. - **{@list} html {/list}:** A loop. - **{#var}:** A loop variable (inside the loop). #### .idealforms('removeFields', names) Remove fields from the form. - **names:** A space separated string of `name` attributes. **Example:** ```javascript $('form').idealforms('removeFields', 'username password hobbies[]'); ``` #### .idealforms('toggleFields', names) Show or hide fields. When the fields are hidden they will be excluded from the validation. - **names:** A space separated string of `name` attributes. **Example:** ```javascript $('form').idealforms('toggleFields', 'username password hobbies[]'); ``` Dynamic Fields adds injection points for `addFields:before`, `addFields:after`, `removeFields` and `toggleFields`. Read about [custom extensions](#custom-extensions) for more info. ### Extension: Steps **Name:** `steps` Steps let you organize your form in sections. Steps expects a container, navigation, wrapper, and at least one step. Using the default options you may build your form like so: ```html