Comparing version 0.9.1 to 0.9.2
@@ -6,3 +6,3 @@ { | ||
"author": "Caolan McMahon", | ||
"version": "0.9.1", | ||
"version": "0.9.2", | ||
"repository": { | ||
@@ -26,3 +26,3 @@ "type": "git", | ||
"formidable": "~1.0.14", | ||
"is": "~0.3.0" | ||
"is": "~2.0.0" | ||
}, | ||
@@ -32,3 +32,3 @@ "devDependencies": { | ||
"testling": "~1.7.0", | ||
"covert": "~0.4.0", | ||
"covert": "~1.0.0", | ||
"jscs": "~1.5.2" | ||
@@ -35,0 +35,0 @@ }, |
226
README.md
@@ -11,3 +11,5 @@ # Forms <sup>[![Version Badge][9]][8]</sup> | ||
npm install forms | ||
```shell | ||
$ npm install forms | ||
``` | ||
@@ -34,3 +36,3 @@ ## Contribute | ||
* [caulagi](https://github.com/caulagi) | ||
* …and 27 more | ||
* …and [many more](https://github.com/caolan/forms/graphs/contributors) | ||
@@ -41,38 +43,44 @@ ## Example | ||
var forms = require('forms'), | ||
fields = forms.fields, | ||
validators = forms.validators; | ||
```javascript | ||
var forms = require('forms'); | ||
var fields = forms.fields; | ||
var validators = forms.validators; | ||
var reg_form = forms.create({ | ||
username: fields.string({ required: true }), | ||
password: fields.password({ required: validators.required('You definitely want a password') }), | ||
confirm: fields.password({ | ||
required: validators.required('don\'t you know your own password?'), | ||
validators: [validators.matchField('password')] | ||
}), | ||
email: fields.email() | ||
}); | ||
var reg_form = forms.create({ | ||
username: fields.string({ required: true }), | ||
password: fields.password({ required: validators.required('You definitely want a password') }), | ||
confirm: fields.password({ | ||
required: validators.required('don\'t you know your own password?'), | ||
validators: [validators.matchField('password')] | ||
}), | ||
email: fields.email() | ||
}); | ||
``` | ||
Rendering a HTML representation of the form: | ||
reg_form.toHTML(); | ||
```javascript | ||
reg_form.toHTML(); | ||
``` | ||
Would produce: | ||
<div class="field required"> | ||
<label for="id_username">Username</label> | ||
<input type="text" name="username" id="id_username" value="test" /> | ||
</div> | ||
<div class="field required"> | ||
<label for="id_password">Password</label> | ||
<input type="password" name="password" id="id_password" value="test" /> | ||
</div> | ||
<div class="field required"> | ||
<label for="id_confirm">Confirm</label> | ||
<input type="password" name="confirm" id="id_confirm" value="test" /> | ||
</div> | ||
<div class="field"> | ||
<label for="id_email">Email</label> | ||
<input type="text" name="email" id="id_email" /> | ||
</div> | ||
```html | ||
<div class="field required"> | ||
<label for="id_username">Username</label> | ||
<input type="text" name="username" id="id_username" value="test" /> | ||
</div> | ||
<div class="field required"> | ||
<label for="id_password">Password</label> | ||
<input type="password" name="password" id="id_password" value="test" /> | ||
</div> | ||
<div class="field required"> | ||
<label for="id_confirm">Confirm</label> | ||
<input type="password" name="confirm" id="id_confirm" value="test" /> | ||
</div> | ||
<div class="field"> | ||
<label for="id_email">Email</label> | ||
<input type="text" name="email" id="id_email" /> | ||
</div> | ||
``` | ||
@@ -84,24 +92,26 @@ You'll notice you have to provide your own form tags and submit button, its | ||
function myView(req, res) { | ||
```javascript | ||
function myView(req, res) { | ||
reg_form.handle(req, { | ||
success: function (form) { | ||
// there is a request and the form is valid | ||
// form.data contains the submitted data | ||
}, | ||
error: function (form) { | ||
// the data in the request didn't validate, | ||
// calling form.toHTML() again will render the error messages | ||
}, | ||
empty: function (form) { | ||
// there was no form data in the request | ||
} | ||
}); | ||
} | ||
``` | ||
reg_form.handle(req, { | ||
success: function (form) { | ||
// there is a request and the form is valid | ||
// form.data contains the submitted data | ||
}, | ||
error: function (form) { | ||
// the data in the request didn't validate, | ||
// calling form.toHTML() again will render the error messages | ||
}, | ||
empty: function (form) { | ||
// there was no form data in the request | ||
} | ||
}); | ||
} | ||
That's it! For more detailed / working examples look in the example folder. | ||
An example server using the form above can be run by doing: | ||
node example/simple.js | ||
```shell | ||
$ node example/simple.js | ||
``` | ||
@@ -111,35 +121,37 @@ ### Bootstrap compatible output | ||
var my_form = forms.create({ | ||
title: fields.string({ | ||
required: true, | ||
widget: widgets.text({ classes: ['input-with-feedback'] }), | ||
errorAfterField: true, | ||
cssClasses: { | ||
label: ['control-label col col-lg-3'] | ||
} | ||
}), | ||
```javascript | ||
var my_form = forms.create({ | ||
title: fields.string({ | ||
required: true, | ||
widget: widgets.text({ classes: ['input-with-feedback'] }), | ||
errorAfterField: true, | ||
cssClasses: { | ||
label: ['control-label col col-lg-3'] | ||
} | ||
}), | ||
description: fields.string({ | ||
errorAfterField: true, | ||
widget: widgets.text({ classes: ['input-with-feedback'] }), | ||
cssClasses: { | ||
label: ['control-label col col-lg-3'] | ||
} | ||
}) | ||
}); | ||
description: fields.string({ | ||
errorAfterField: true, | ||
widget: widgets.text({ classes: ['input-with-feedback'] }), | ||
cssClasses: { | ||
label: ['control-label col col-lg-3'] | ||
} | ||
}) | ||
}); | ||
var bootstrapField = function (name, object) { | ||
object.widget.classes = object.widget.classes || []; | ||
object.widget.classes.push('form-control'); | ||
var bootstrapField = function (name, object) { | ||
var label = object.labelHTML(name); | ||
var error = object.error ? '<div class="alert alert-error">' + object.error + '</div>' : ''; | ||
var widget = object.widget.toHTML(name, object); | ||
return '<div class="form-group">' + label + widget + error + '</div>'; | ||
}; | ||
``` | ||
object.widget.classes = object.widget.classes || []; | ||
object.widget.classes.push('form-control'); | ||
var label = object.labelHTML(name); | ||
var error = object.error ? '<div class="alert alert-error">' + object.error + '</div>' : ''; | ||
var widget = object.widget.toHTML(name, object); | ||
return '<div class="form-group">' + label + widget + error + '</div>'; | ||
}; | ||
And while rendering it: | ||
form.toHTML(bootstrapField); | ||
```javascript | ||
form.toHTML(bootstrapField); | ||
``` | ||
@@ -217,2 +229,3 @@ ## Available types | ||
### forms.create(fields) | ||
Converts a form definition (an object literal containing field objects) into a | ||
@@ -222,7 +235,10 @@ form object. | ||
#### forms.create(fields, options) | ||
Forms can be created with an optional "options" object as well. | ||
#### Supported options: | ||
* `validatePastFirstError`: `true`, otherwise assumes `false` | ||
* If `false`, the first validation error will halt form validation. | ||
* If `true`, all fields will be validated. | ||
* If `false`, the first validation error will halt form validation. | ||
* If `true`, all fields will be validated. | ||
@@ -234,12 +250,15 @@ | ||
* fields - Object literal containing the field objects passed to the create | ||
* ``fields`` - Object literal containing the field objects passed to the create | ||
function | ||
#### form.handle(req, callbacks) | ||
Inspects a request or object literal and binds any data to the correct fields. | ||
#### form.bind(data) | ||
Binds data to correct fields, returning a new bound form object. | ||
#### form.toHTML(iterator) | ||
Runs toHTML on each field returning the result. If an iterator is specified, | ||
@@ -257,7 +276,8 @@ it is called for each field with the field name and object as its arguments, | ||
* data - Object containing all the parsed data keyed by field name | ||
* fields - Object literal containing the field objects passed to the create | ||
* ``data`` - Object containing all the parsed data keyed by field name | ||
* ``fields`` - Object literal containing the field objects passed to the create | ||
function | ||
#### form.validate(callback) | ||
Calls validate on each field in the bound form and returns the resulting form | ||
@@ -267,2 +287,3 @@ object to the callback. | ||
#### form.isValid() | ||
Checks all fields for an error attribute. Returns false if any exist, otherwise | ||
@@ -272,2 +293,3 @@ returns true. | ||
#### form.toHTML(iterator) | ||
Runs toHTML on each field returning the result. If an iterator is specified, | ||
@@ -283,13 +305,13 @@ it is called for each field with the field name and object as its arguments, | ||
* label - Optional label text which overrides the default | ||
* required - Boolean describing whether the field is mandatory | ||
* validators - An array of functions which validate the field data | ||
* widget - A widget object to use when rendering the field | ||
* id - An optional id to override the default | ||
* choices - A list of options, used for multiple choice fields | ||
* cssClasses - A list of CSS classes for label and field wrapper | ||
* hideError - if true, errors won't be rendered automatically | ||
* errorAfterField - if true, the error message will be displayed after the field, rather than before | ||
* fieldsetClasses - for widgets with a fieldset (multipleRadio and multipleCheckbox), set classes for the fieldset | ||
* legendClasses - for widgets with a fieldset (multipleRadio and multipleCheckbox), set classes for the fieldset's legend | ||
* ``label`` - Optional label text which overrides the default | ||
* ``required`` - Boolean describing whether the field is mandatory | ||
* ``validators`` - An array of functions which validate the field data | ||
* ``widget`` - A widget object to use when rendering the field | ||
* ``id`` - An optional id to override the default | ||
* ``choices`` - A list of options, used for multiple choice fields | ||
* ``cssClasses`` - A list of CSS classes for label and field wrapper | ||
* ``hideError`` - if true, errors won't be rendered automatically | ||
* ``errorAfterField`` - if true, the error message will be displayed after the field, rather than before | ||
* ``fieldsetClasses`` - for widgets with a fieldset (multipleRadio and multipleCheckbox), set classes for the fieldset | ||
* ``legendClasses`` - for widgets with a fieldset (multipleRadio and multipleCheckbox), set classes for the fieldset's legend | ||
@@ -339,5 +361,5 @@ #### field.parse(rawdata) | ||
* value - The raw value from the request data | ||
* data - The request data coerced to the correct format for this field | ||
* error - An error message if the field fails validation | ||
* ``value`` - The raw value from the request data | ||
* ``data`` - The request data coerced to the correct format for this field | ||
* ``error`` - An error message if the field fails validation | ||
@@ -355,5 +377,5 @@ #### validate(callback) | ||
* classes - Custom classes to add to the rendered widget | ||
* labelClasses - Custom classes to add to the choices label when applicable (multipleRadio and multipleCheckbox) | ||
* type - A string representing the widget type, e.g. 'text' or 'checkbox' | ||
* ``classes`` - Custom classes to add to the rendered widget | ||
* ``labelClasses`` - Custom classes to add to the choices label when applicable (multipleRadio and multipleCheckbox) | ||
* ``type`` - A string representing the widget type, e.g. 'text' or 'checkbox' | ||
@@ -379,7 +401,7 @@ #### toHTML(name, field) | ||
[1]: https://travis-ci.org/caolan/forms.png | ||
[1]: https://travis-ci.org/caolan/forms.svg | ||
[2]: https://travis-ci.org/caolan/forms | ||
[3]: https://david-dm.org/caolan/forms.png | ||
[3]: https://david-dm.org/caolan/forms.svg | ||
[4]: https://david-dm.org/caolan/forms | ||
[5]: https://david-dm.org/caolan/forms/dev-status.png | ||
[5]: https://david-dm.org/caolan/forms/dev-status.svg | ||
[6]: https://david-dm.org/caolan/forms#info=devDependencies | ||
@@ -386,0 +408,0 @@ [7]: https://nodei.co/npm/forms.png?downloads=true&stars=true |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
397
161810