elclanrs 23892b6625 initial commit | 10 yıl önce | |
---|---|---|
css | 10 yıl önce | |
img | 10 yıl önce | |
js | 10 yıl önce | |
styl | 10 yıl önce | |
.gitignore | 10 yıl önce | |
README.md | 10 yıl önce | |
ajax.php | 10 yıl önce | |
compile.sh | 10 yıl önce | |
index.php | 10 yıl önce | |
package.json | 10 yıl önce |
The best forms just got better! Ideal Forms 3 is smaller, faster, and more extensible.
Support: IE9+ and all modern browsers.
Demo:
Ideal Forms 3 is not compatible with version 2. Here's what's new:
css/jquery.idealforms.css
stylesheetjs/out/jquery.idealforms.min.js
pluginhtml
tag with:<!--[if IE 9]> <html class="ie9" lang="en"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html lang="en"> <!--<![endif]-->
$('form').idealforms({ options });
defaults = {
field: '.field',
error: '.error',
iconHtml: '<i/>',
iconClass: 'icon',
invalidClass: 'invalid',
validClass: 'valid',
silentLoad: true,
onValidate: $.noop,
onSubmit: $.noop,
rules: {}
}
The field container for custom markup.
The error container for custom markup.
The element to use as icon. Set to false
to disable icons.
The class for the icon element.
The class that will be added to invalid fields.
The class that will be added to valid fields.
Initialize the form silently, otherwise focus the first invalid input.
Callback that runs after an input attempts to validate.
Callback that runs when the form is submitted.
You can get started quickly using Ideal Forms' default markup:
<form class="idealforms" novalidate action="/" method="post">
<!-- Text -->
<div class="field">
<label class="main">Username:</label>
<input name="username" type="text">
<span class="error"></span>
</div>
<!-- File -->
<div class="field">
<label class="main">Picture:</label>
<input id="picture" name="picture" type="file" multiple>
<span class="error"></span>
</div>
<!-- Radio -->
<div class="field">
<label class="main">Sex:</label>
<p class="group">
<label><input name="sex" type="radio" value="male">Male</label>
<label><input name="sex" type="radio" value="female">Female</label>
</p>
<span class="error"></span>
</div>
<!-- Checkbox -->
<div class="field">
<label class="main">Hobbies:</label>
<p class="group">
<label><input name="hobbies[]" type="checkbox" value="football">Football</label>
<label><input name="hobbies[]" type="checkbox" value="basketball">Basketball</label>
<label><input name="hobbies[]" type="checkbox" value="dancing">Dancing</label>
</p>
<span class="error"></span>
</div>
<!-- Select -->
<div class="field">
<label class="main">Options:</label>
<select name="options" id="">
<option value="default">– Select an option –</option>
<option value="1">One</option>
<option value="2">Two</option>
</select>
<span class="error"></span>
</div>
<!-- Textarea -->
<div class="field">
<label class="main">Comments:</label>
<textarea name="comments" cols="30" rows="10"></textarea>
<span class="error"></span>
</div>
<!-- Button -->
<button type="submit">Submit</button>
</form>
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:
name
attributes; this is a limitation by design.Then you have to tell Ideal Forms that you want to use custom markup. You can specify these options when initializing the plugin:
$('form').idealforms({
field: '.myfield', // selector
error: '.myerror', // selector
validClass: 'myvalid', // class name
invalidClass: 'myinvalid' // class name
});
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:
$('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:
$('form').idealforms('addRules', {
'comments': 'required minmax:50:200'
});
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
.
min
and max
. Usually combined with number
or digits
.min
characters long.max
characters long.min
and max
characters long.min
checkboxes or radios selected.max
checkboxes or radios selected.default
is the value of the default option.ext
as you want.name
is the name of the field.To call a method run idealforms
on the form and pass the method and arguments:
$('form').idealforms('method', arg1, arg2, ...);
See Adding Rules
Returns a jQuery collection of all invalid fields. Access the length
to check how many fields are invalid.
Focus the first invalid field.
Check if the input with name
attribute is valid.
Reset all onputs to zero. That means emptying all the values of text inputs, unchecking all checkboxes and radios, and reverting selects to their default option.
Ideal Forms 3 has been re-designed to be able to extend the core easily. Read on 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.
$('form').idealforms({
disabledExtensions: 'dynamicFields steps customInputs ajax'
});
Dynamic Fields extends core with the following methods:
Add fields to the form.
name
attribute. The value of the object is another object that can contain any of the following options (* are required):{
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:
$('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):
<!-- Base -->
<div class="field" {class}>
<label class="main">{label}</label>
{field}
<span class="error"></span>
</div>
<!-- Text -->
<input name="{name}" type="{subtype}" value="{value}" {attrs}>
<!-- File -->
<input id="{name} "name="{name}" type="file" value="{value}" {attrs}>
<!-- Textarea -->
<textarea name="{name}" {attrs}>{text}</textarea>
<!-- Group -->
<p class="group">
{@list}
<label><input name="{name}" type="{subtype}" value="{#value}" {#attrs}>{#text}</label>
{/list}
</p>
<!-- Select -->
<select name={name}>
{@list}
<option value="{#value}">{#text}</option>
{/list}
</select>
$('form').idealforms('templates', {
templates: {
base: '...',
text: '...',
file: '...',
textarea: '...',
group: '...',
select: '...'
}
});
The templating rules are:
Remove fields from the form.
name
attributes.Example:
$('form').idealforms('removeFields', 'username password hobbies[]');
Show or hide fields. When the fields are hidden they will be excluded from the validation.
name
attributes.Example:
$('form').idealforms('toggleFields', 'username password hobbies[]');
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:
<div class="idealsteps-container">
<nav class="idealsteps-nav"></nav>
<form>
<div class="idealsteps-wrap">
<!-- Step 1 -->
<section class="idealsteps-step">
<!-- Form fields -->
</section>
<!-- Step 2 -->
<section class="idealsteps-step">
<!-- Form fields -->
</section>
<!-- Step 3 -->
<section class="idealsteps-step">
<!-- Form fields -->
</section>
</div>
</form>
</div>
Steps adds the following options to Ideal Forms:
{
stepsContainer: 'idealsteps-container', // the main container
stepsOptions: { // the options for the Steps extension
nav: '.idealsteps-nav',
navItems: 'li',
// Build nav items as "Step 1", "Step 2"... automatically
// Set to `false` to use your own custom markup
buildNavItems: true,
wrap: '.idealsteps-wrap',
step: '.idealsteps-step',
activeClass: 'idealsteps-step-active',
before: null, // runs before changing steps
after: null, // runs after changing steps
fadeSpeed: 0
}
}
Steps provides these methods:
Go to the step number index
. Indices start at 0
.
Go to the previous step. It wraps around.
Go to the next step. It wraps around.
Go to the first step.
Go to the last step.
Adds custom checkboxes, radios and file inputs (yes!) seamlessly. The custom select menu has been dropped from Ideal Forms 3; it was proving to be a pain to maintain, and there are much better alternatives, such as Select2 if you need them.
This extension has no additional options or methods.
Adds an ajax
rule. First specify a URL on your input:
<input type="text" name="username" data-idealforms-ajax="test-username.php"/>
Then add the rule to the field always at last.
$('form').idealforms({
rules: {
'username': 'required username ajax'
}
});
The ajax filter will read the URL and send a POST request to the server. The server side script must return a JSON encoded true
(passes validation) or false
(fails validation). for example in PHP:
<?php
echo json_encode(true);
If the response gets delayed the field will switch into "ajax mode" by showing a loading icon until the response comes back.
Adds a datepicker to date inputs. You must include jQuery UI first, then add the class datepicker
to your date inputs:
<input name="event" type="text" placeholder="mm/dd/yyyy" class="datepicker">
Now you can add the date
rule:
$('form').idealforms({
rules: {
'event': 'date'
}
});
The default format is mm/dd/yyyy
for Ideal Forms and mm/dd/yy
for the jQuery UI datepicker. Note that Ideal Forms represents years with yyyy
not yy
, but the formats are interchangeable.
$('form').idealforms({
rules: {
'event': 'date:yyyy-mm-dd'
}
});
$('.datepicker').datepicker('option', 'dateFormat', 'yy-mm-dd');
You can add rules by extending the global rules
object:
$.extend($.idealforms.rules, {
ruleRegex: /abc/g,
// The rule is added as "ruleFunction:arg1:arg2"
// @param input {Node}
ruleFunction: function(input, value, arg1, arg2, ...) {
}
});
After you add a rule you must add an error for it, by extending the global errors
object. Don't forget this step:
$.extend($.idealforms.errors, {
ruleRegex: 'Must be a valid value for this rule',
ruleFunction: 'Must be a valid value. {0}, {1}'
});
If the rule is a function that takes rule parameters pass the parameters as {0}
, {1}
, etc. If you want to print all the parameters use {*}
where the default separator is a comma but you can use your own like {*~}
where ~
is the custom separator.
$.idealforms.addExtension({
name: 'extensionName',
options: {
option: 'default'
},
methods: {
// @extend
_init: function() {
},
newMethod: function() {
}
}
});
Ideal Forms attaches an instance of itself to your form(s). To access the instance (prototype) use data
:
var instance = $('form').data('idealforms');
Now you an use methods like so:
instance.reset(); // reset the form
Ideal Forms 3 is built using some popular NodeJS tools. This is what you'll need to install globally:
Then clone the repo, cd
into the folder and run npm install
to install dependencies.
Finally run watch -c sh compile.sh
to watch for changes and compile. Now you're ready to edit files and help make Ideal Forms better, or create your own fork.
If you want to test ajax make sure to run it on your localhost.
Enjoy ;)