@@ -3,6 +3,13 @@ Angular Schema Form
33
44Generate 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+
613Schema Form is a set of AngularJS directives (and a service..) that can create a form directly from a json schema
714definition and also validate against that schema. The defaults may be fine for a lot cases, but you can also
815customize 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
1825does, 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-
2528Basic 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+
5669Form types
5770----------
5871Schema 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
7791Default 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+
182221Per 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
185224string that will be used instead on any validation error.
186225
187226If you need more fine grained control you can supply an object instead with keys matching the error
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
203270Specific options per type
204271-------------------------
@@ -223,7 +290,8 @@ A *conditional* is exactly the same as a *section*, i.e. a ```<div>``` with othe
223290it, hence they need an ``` items ``` property. They also need a ``` condition ``` which is
224291a string with an angular expression. If that expression evaluates as thruthy the * conditional*
225292will 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
227295parts of a form depending on another form control.
228296
229297ex. 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
301369string 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+ ```
0 commit comments