Skip to content

Commit 1f39467

Browse files
committed
Merge branch 'development' into feature/datepicker
Conflicts: dist/bootstrap-decorator.min.js dist/schema-form.min.js src/bootstrap-example.html src/directives/decorators/bootstrap/bootstrap-decorator.js
2 parents ba98071 + daa043d commit 1f39467

23 files changed

+411
-68
lines changed

CHANGELOG

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
v0.2.0
2+
------
3+
* postProcess function in schemaForm provider
4+
* New form option: Inline feedback icons in fields.
5+
* New form option: onChange a function or expression that triggers in the same
6+
way as ng-change.
7+
* Removed dependencies on jQuery (thanks @zackbloom and @tsing!)
8+
9+
v0.1.0
10+
------
11+
We're celebrating actual useful functionality by bumping minor version, yay!
12+
* ```radios``` and ```radiobuttons``` supports, works the same but looks different.
13+
* Added ```conditional``` type to hide/show parts of a form.
14+
* Bugfixes
15+
16+
117
v0.0.4
218
------
319
* Fieldsets now properly merge schema defaults.

README.md

Lines changed: 133 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ Angular Schema Form
33

44
Generate forms from a JSON schema, with AngularJS!
55

6+
### [Try out the example page](http://textalk.github.io/angular-schema-form/src/bootstrap-example.html)
7+
...where you can edit the schema or the form definition and see what comes out!
8+
9+
10+
What is it?
11+
----------
12+
613
Schema Form is a set of AngularJS directives (and a service..) that can create a form directly from a json schema
714
definition and also validate against that schema. The defaults may be fine for a lot cases, but you can also
815
customize it, changing order and type of fields.
@@ -18,10 +25,6 @@ Another thing that sets Schema Form apart is that it, at the moment, doesn't imp
1825
does, nor have any documentation! Which of course we hope to remedy soon.
1926

2027

21-
Try out the [example](http://textalk.github.io/angular-schema-form/src/bootstrap-example.html) where you can edit
22-
the schema or the form definition and see what comes out!
23-
24-
2528
Basic Usage
2629
-----------
2730

@@ -38,21 +41,31 @@ function FormController($scope) {
3841
title: {
3942
type: "string",
4043
enum: ['dr','jr','sir','mrs','mr','NaN','dj']
44+
}
4145
}
4246
};
4347

4448
$scope.form = [
4549
"*",
4650
{
4751
type: "submit",
48-
title: "Save",
52+
title: "Save"
4953
}
5054
];
5155

5256
$scope.data = {};
5357
}
5458
```
5559

60+
61+
Contributing
62+
------------
63+
64+
All contributions are welcome! We're trying to use [git flow](http://danielkummer.github.io/git-flow-cheatsheet/)
65+
so please base any merge request on the **development** branch instead of **master**.
66+
67+
68+
5669
Form types
5770
----------
5871
Schema Form currently supports the following form field types:
@@ -71,7 +84,8 @@ Schema Form currently supports the following form field types:
7184
| select | a select (single value)|
7285
| submit | a submit button |
7386
| button | a button |
74-
87+
| radios | radio buttons |
88+
| radiobuttons | radio buttons with bootstrap buttons |
7589

7690

7791
Default form types
@@ -173,15 +187,40 @@ General options most field types can handle:
173187
title: "Street", //Title of field, taken from schema if available
174188
notitle: false, //Set to true to hide title
175189
description: "Street name", //A description, taken from schema if available
176-
validationMessage: "Oh noes, please write a proper address" //A custom validation error message
190+
validationMessage: "Oh noes, please write a proper address", //A custom validation error message
191+
onChange: "valueChanged(form.key,modelValue)", //onChange event handler, expression or function
192+
feedback: false //inline feedback icons
177193
}
178194
```
179195

180-
Validation Messages
181-
-------------------
196+
### onChange
197+
The ```onChange``` option can be used with most fields and its value should be
198+
either an angular expression, as a string, or a function. If its an expression
199+
it will be evaluated in the parent scope of the ```sf-schema``` directive with
200+
the special locals ```modelValue``` and ```form```. If its a function that will
201+
be called with ```modelValue``` and ```form``` as first and second arguments.
202+
203+
ex.
204+
```javascript
205+
$scope.form = [
206+
{
207+
key: "name",
208+
onChange: "updated(modelValue,form)"
209+
},
210+
{
211+
key: "password",
212+
onChange: function(modelValue,form) {
213+
console.log("Password is",modelValue);
214+
}
215+
}
216+
];
217+
```
218+
219+
### Validation Messages
220+
182221
Per default all error messages but "Required" comes from the schema validator
183222
[tv4](https://github.com/geraintluff/tv4), this might or might not work for you.
184-
If you supply a ´´´validationMessage´´´ proṕerty in the form definition, and if its value is a
223+
If you supply a ```validationMessage``` property in the form definition, and if its value is a
185224
string that will be used instead on any validation error.
186225

187226
If you need more fine grained control you can supply an object instead with keys matching the error
@@ -199,6 +238,34 @@ Ex.
199238
}
200239
```
201240

241+
### Inline feedback icons
242+
*input* and *textarea* based fields get inline status icons by default. A check
243+
when everything is valid and a cross when there are validation errors.
244+
245+
This can be turned off or configured to other icons. To turn off just
246+
set ```feedback``` to false. If set to a string that string is evaluated by
247+
a ```ngClass``` in the decorators scope. If not set att all the default value
248+
is ```{ 'glyphicon': true, 'glyphicon-ok': hasSuccess(), 'glyphicon-remove': hasError() }```
249+
250+
ex. displaying an asterisk on required fields
251+
```javascript
252+
$sope.form = [
253+
{
254+
key: "name",
255+
feedback: "{ 'glyphicon': true, 'glyphicon-asterisk': form.requires && !hasSuccess && !hassError() ,'glyphicon-ok': hasSuccess(), 'glyphicon-remove': hasError() }"
256+
}
257+
```
258+
259+
Useful things in the decorators scope are
260+
261+
| Name | Description|
262+
|:---------------|:----------:|
263+
| hasSuccess() | *true* if field is valid and not pristine |
264+
| hasError() | *true* if field is invalid and not pristine |
265+
| ngModel | The controller of the ngModel directive, ex. ngModel.$valid |
266+
| form | The form definition for this field |
267+
268+
202269

203270
Specific options per type
204271
-------------------------
@@ -223,7 +290,8 @@ A *conditional* is exactly the same as a *section*, i.e. a ```<div>``` with othe
223290
it, hence they need an ```items``` property. They also need a ```condition``` which is
224291
a string with an angular expression. If that expression evaluates as thruthy the *conditional*
225292
will be rendered into the DOM otherwise not. The expression is evaluated in the parent scope of
226-
the ```sf-schema``` directive (the same as onClick on buttons). This is useful for hiding/showing
293+
the ```sf-schema``` directive (the same as onClick on buttons) but with access to the current model
294+
under the name ```model```. This is useful for hiding/showing
227295
parts of a form depending on another form control.
228296

229297
ex. A checkbox that shows an input field for a code when checked
@@ -255,7 +323,7 @@ function FormCtrl($scope) {
255323
"eligible",
256324
{
257325
type: "conditional",
258-
condition: "person.eligible",
326+
condition: "person.eligible", //or "model.eligable"
259327
items: [
260328
"code"
261329
]
@@ -299,7 +367,7 @@ and the value is the title of the option.
299367

300368
*button* can have a ```onClick``` attribute that either, as in JSON Form, is a function *or* a
301369
string with an angular expression, as with ng-click. The expression is evaluated in the parent scope of
302-
the ```sf-schema``` directive.
370+
the ```sf-schema``` directive.
303371

304372
```javascript
305373
[
@@ -308,3 +376,55 @@ the ```sf-schema``` directive.
308376
[
309377
```
310378

379+
### radios and radiobuttons
380+
Both type *radios* and *radiobuttons* work the same way, they take a titleMap
381+
and renders ordinary radio buttons or bootstrap 3 buttons inline. It's a
382+
cosmetic choice.
383+
384+
Ex.
385+
```javascript
386+
function FormCtrl($scope) {
387+
$scope.schema = {
388+
type: "object",
389+
properties: {
390+
choice: {
391+
type: "string",
392+
enum: ["one","two"]
393+
}
394+
}
395+
};
396+
397+
$scope.form = [
398+
{
399+
key: "choice",
400+
type: "radiobuttons",
401+
titleMap: {
402+
one: "One",
403+
two: "More..."
404+
}
405+
}
406+
];
407+
}
408+
```
409+
410+
411+
Post process function
412+
---------------------
413+
414+
If you like to use ```["*"]``` as a form, or aren't in control of the form definitions
415+
but really need to change or add something you can register a *post process*
416+
function with the ```schemaForm``` service provider. The post process function
417+
gets one argument, the final form merged with the defaults from the schema just
418+
before it's rendered, and should return a form.
419+
420+
Ex. Reverse all forms
421+
```javascript
422+
angular.module('myModule').config(function(schemaFormProvider){
423+
424+
schemaForm.postProcess(function(form){
425+
form.reverse();
426+
return form;
427+
})
428+
429+
});
430+
```

bower.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{
22
"name": "angular-schema-form",
3-
"version": "0.0.4",
3+
"main": [
4+
"dist/schema-form.min.js",
5+
"dist/bootstrap-decorator.min.js"
6+
],
7+
"version": "0.2.0",
48
"authors": [
59
"Textalk",
610
"David Jensen <david.lgj@gmail.com>"

0 commit comments

Comments
 (0)