Skip to content

Commit a7d0579

Browse files
committed
Fix for validation bug in radios
And also some convinience methods on the builder.
1 parent 21db7fd commit a7d0579

File tree

10 files changed

+245
-102
lines changed

10 files changed

+245
-102
lines changed

dist/bootstrap-decorator.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/bootstrap-decorator.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/schema-form.js

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,13 @@ angular.module('schemaForm').provider('sfBuilder', ['sfPathProvider', function(s
299299
}
300300
};
301301
this.builders = builders;
302+
var stdBuilders = [
303+
builders.sfField,
304+
builders.ngModel,
305+
builders.ngModelOptions,
306+
builders.condition
307+
];
308+
this.stdBuilders = stdBuilders;
302309

303310
this.$get = ['$templateCache', 'schemaFormDecorators', 'sfPath', function($templateCache, schemaFormDecorators, sfPath) {
304311

@@ -408,6 +415,7 @@ angular.module('schemaForm').provider('sfBuilder', ['sfPathProvider', function(s
408415

409416
},
410417
builder: builders,
418+
stdBuilders: stdBuilders,
411419
internalBuild: build
412420
};
413421
}];
@@ -435,8 +443,16 @@ angular.module('schemaForm').provider('schemaFormDecorators',
435443
return decorator['default'].template;
436444
};
437445

438-
var createDirective = function(name) {
439-
$compileProvider.directive(name,
446+
/**************************************************
447+
* DEPRECATED *
448+
* The new builder and sf-field is preferred, but *
449+
* we keep this in during a transitional period *
450+
* so that add-ons that don't use the new builder *
451+
* works. *
452+
**************************************************/
453+
//TODO: Move to a compatability extra script.
454+
var createDirective = function(name) {
455+
$compileProvider.directive(name,
440456
['$parse', '$compile', '$http', '$templateCache', '$interpolate', '$q', 'sfErrorMessage',
441457
'sfPath','sfSelect',
442458
function($parse, $compile, $http, $templateCache, $interpolate, $q, sfErrorMessage,
@@ -541,35 +557,20 @@ angular.module('schemaForm').provider('schemaFormDecorators',
541557
return (expression && $interpolate(expression)(locals));
542558
};
543559

544-
//This works since we get the ngModel from the array or the schema-validate directive.
560+
//This works since we ot the ngModel from the array or the schema-validate directive.
545561
scope.hasSuccess = function() {
546562
if (!scope.ngModel) {
547563
return false;
548564
}
549-
if (scope.options && scope.options.pristine &&
550-
scope.options.pristine.success === false) {
551-
return scope.ngModel.$valid &&
552-
!scope.ngModel.$pristine && !scope.ngModel.$isEmpty(scope.ngModel.$modelValue);
553-
} else {
554-
return scope.ngModel.$valid &&
565+
return scope.ngModel.$valid &&
555566
(!scope.ngModel.$pristine || !scope.ngModel.$isEmpty(scope.ngModel.$modelValue));
556-
}
557567
};
558568

559569
scope.hasError = function() {
560570
if (!scope.ngModel) {
561571
return false;
562572
}
563-
if (!scope.options || !scope.options.pristine || scope.options.pristine.errors !== false) {
564-
// Show errors in pristine forms. The default.
565-
// Note that "validateOnRender" option defaults to *not* validate initial form.
566-
// so as a default there won't be any error anyway, but if the model is modified
567-
// from the outside the error will show even if the field is pristine.
568-
return scope.ngModel.$invalid;
569-
} else {
570-
// Don't show errors in pristine forms.
571-
return scope.ngModel.$invalid && !scope.ngModel.$pristine;
572-
}
573+
return scope.ngModel.$invalid && !scope.ngModel.$pristine;
573574
};
574575

575576
/**
@@ -819,17 +820,22 @@ angular.module('schemaForm').provider('schemaFormDecorators',
819820

820821

821822
/**
822-
* Create a decorator directive and its sibling "manual" use decorators.
823-
* The directive can be used to create form fields or other form entities.
824-
* It can be used in conjunction with <schema-form> directive in which case the decorator is
825-
* given it's configuration via a the "form" attribute.
823+
* Define a decorator. A decorator is a set of form types with templates and builder functions
824+
* that help set up the form.
826825
*
827-
* ex. Basic usage
828-
* <sf-decorator form="myform"></sf-decorator>
829-
**
830826
* @param {string} name directive name (CamelCased)
831827
* @param {Object} fields, an object that maps "type" => `{ template, builder, replace}`.
832828
attributes `builder` and `replace` are optional, and replace defaults to true.
829+
830+
`template` should be the key of the template to load and it should be pre-loaded
831+
in `$templateCache`.
832+
833+
`builder` can be a function or an array of functions. They will be called in
834+
the order they are supplied.
835+
836+
`replace` (DEPRECATED) is for backwards compatability. If false the builder
837+
will use the "old" way of building that form field using a <sf-decorator>
838+
directive.
833839
*/
834840
this.defineDecorator = function(name, fields) {
835841
decorators[name] = {'__name': name}; // TODO: this feels like a hack, come up with a better way.
@@ -847,6 +853,7 @@ angular.module('schemaForm').provider('schemaFormDecorators',
847853
};
848854

849855
/**
856+
* DEPRECATED
850857
* Creates a directive of a decorator
851858
* Usable when you want to use the decorators without using <schema-form> directive.
852859
* Specifically when you need to reuse styling.
@@ -861,6 +868,7 @@ angular.module('schemaForm').provider('schemaFormDecorators',
861868
this.createDirective = createManualDirective;
862869

863870
/**
871+
* DEPRECATED
864872
* Same as createDirective, but takes an object where key is 'type' and value is 'templateUrl'
865873
* Useful for batching.
866874
* @param {Object} templates
@@ -883,6 +891,7 @@ angular.module('schemaForm').provider('schemaFormDecorators',
883891

884892

885893
/**
894+
* DEPRECATED use defineAddOn() instead.
886895
* Adds a mapping to an existing decorator.
887896
* @param {String} name Decorator name
888897
* @param {String} type Form type for the mapping
@@ -900,6 +909,25 @@ angular.module('schemaForm').provider('schemaFormDecorators',
900909
}
901910
};
902911

912+
/**
913+
* Adds an add-on to an existing decorator.
914+
* @param {String} name Decorator name
915+
* @param {String} type Form type for the mapping
916+
* @param {String} url The template url
917+
* @param {Function|Array} builder (optional) builder function(s),
918+
*/
919+
this.defineAddOn = function(name, type, url, builder) {
920+
if (decorators[name]) {
921+
decorators[name][type] = {
922+
template: url,
923+
builder: builder,
924+
replace: true
925+
};
926+
}
927+
};
928+
929+
930+
903931
//Service is just a getter for directive templates and rules
904932
this.$get = function() {
905933
return {

dist/schema-form.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Example with custom submit buttons:
9999
Handling Submit
100100
---------------
101101
Schema Form does not care what you do with your data, to handle form submit
102-
the recomended way is to use the `ng-submit` directive. It's also recomended
102+
the recommended way is to use the `ng-submit` directive. It's also recommended
103103
to use a `name` attribute on your form so you can access the
104104
[FormController](https://code.angularjs.org/1.3.0-beta.15/docs/api/ng/type/form.FormController)
105105
and check if the form is valid or not.
@@ -161,7 +161,7 @@ Updating Form
161161
-------------
162162
163163
Schema Form watches `sf-form` and `sf-schema` and will redraw the form if one or both changes, but
164-
only if they change completly, i.e. not the same object and/or form instance. For performance
164+
only if they change completely, i.e. not the same object and/or form instance. For performance
165165
reasons we have opted to not watch schema and form deeply. So if you have updated a part of the
166166
schema or the form definition you can trigger a redraw by issuing the event `schemaFormRedraw`.
167167
@@ -187,15 +187,13 @@ attribute which should be placed along side `sf-schema`.
187187
`sf-options` takes an object with the following possible attributes.
188188
189189
190-
| Attribute | Type | |
191-
|:--------------|:------|:-------------------|
192-
| supressPropertyTitles | boolean |by default schema form uses the property name in the schema as a title if none is specified, set this to true to disable that behavior |
193-
| formDefaults | object | an object that will be used as a default for all form definitions |
194-
| validationMessage | object or function | Object or a function that will be used as default validation message for all fields. See [Validation Messages](#validation-messages) for details. |
195-
| setSchemaDefaults | boolean | Should schema defaults be set on model. |
196-
| destroyStrategy | string | the default strategy to use for cleaning the model when a form element is removed. see [destroyStrategy](#destroyStrategy) below |
197-
| pristine | Object `{errors ,success}` | Sets if errors and success states should be visible when form field are `$pristine`. Default is `{errors: true, success: true}` |
198-
| validateOnRender | boolean | Should form be validated on initial render? Default `false` |
190+
| Attribute | |
191+
|:--------------|:------------------------|
192+
| supressPropertyTitles | by default schema form uses the property name in the schema as a title if none is specified, set this to true to disable that behavior |
193+
| formDefaults | an object that will be used as a default for all form definitions |
194+
| validationMessage | an object or a function that will be used as default validation message for all fields. See [Validation Messages](#validation-messages) for details. |
195+
| setSchemaDefaults | boolean, set to false an no defaults from the schema will be set on the model. |
196+
| destroyStrategy | the default strategy to use for cleaning the model when a form element is removed. see [destroyStrategy](#destroyStrategy) below |
199197
200198
*formDefaults* is mostly useful for setting global [ngModelOptions](#ngmodeloptions)
201199
i.e. changing the entire form to validate on blur.

src/directives/decorators/bootstrap/radios-inline.html

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
<div class="form-group schema-form-radios-inline {{form.htmlClass}}"
22
ng-class="{'has-error': form.disableErrorState !== true && hasError(), 'has-success': form.disableSuccessState !== true && hasSuccess()}">
3-
<label class="control-label {{form.labelHtmlClass}}" ng-show="showTitle()">{{form.title}}</label>
3+
<label ng-model="$$value$$"
4+
ng-model-options="form.ngModelOptions"
5+
schema-validate="form"
6+
class="control-label {{form.labelHtmlClass}}" ng-show="showTitle()">{{form.title}}</label>
47
<div>
5-
<label class="radio-inline" ng-repeat="item in form.titleMap" >
6-
<input type="radio"
7-
class="{{form.fieldHtmlClass}}"
8-
sf-changed="form"
9-
ng-disabled="form.readonly"
10-
ng-model="$$value$$"
11-
schema-validate="form"
12-
ng-value="item.value"
13-
name="{{form.key.join('.')}}">
14-
<span ng-bind-html="item.name"></span>
8+
<label class="radio-inline" ng-repeat="item in form.titleMap">
9+
<input type="radio"
10+
class="{{form.fieldHtmlClass}}"
11+
sf-changed="form"
12+
ng-disabled="form.readonly"
13+
ng-model="$$value$$"
14+
ng-value="item.value"
15+
name="{{form.key.join('.')}}">
16+
<span ng-bind-html="item.name"></span>
1517
</label>
1618
</div>
1719
<div class="help-block" sf-message="form.description"></div>

src/directives/decorators/bootstrap/radios.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<div class="form-group schema-form-radios {{form.htmlClass}}" ng-class="{'has-error': form.disableErrorState !== true && hasError(), 'has-success': form.disableSuccessState !== true && hasSuccess()}">
1+
<div class="form-group schema-form-radios {{form.htmlClass}}"
2+
ng-class="{'has-error': form.disableErrorState !== true && hasError(), 'has-success': form.disableSuccessState !== true && hasSuccess()}">
23
<label ng-model="$$value$$"
34
ng-model-options="form.ngModelOptions"
45
schema-validate="form"
@@ -10,7 +11,6 @@
1011
sf-changed="form"
1112
ng-disabled="form.readonly"
1213
ng-model="$$value$$"
13-
ng-model-options="form.ngModelOptions"
1414
ng-value="item.value"
1515
name="{{form.key.join('.')}}">
1616
<span ng-bind-html="item.name"></span>

src/services/builder.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ angular.module('schemaForm').provider('sfBuilder', ['sfPathProvider', function(s
157157
}
158158
};
159159
this.builders = builders;
160+
var stdBuilders = [
161+
builders.sfField,
162+
builders.ngModel,
163+
builders.ngModelOptions,
164+
builders.condition
165+
];
166+
this.stdBuilders = stdBuilders;
160167

161168
this.$get = ['$templateCache', 'schemaFormDecorators', 'sfPath', function($templateCache, schemaFormDecorators, sfPath) {
162169

@@ -266,6 +273,7 @@ angular.module('schemaForm').provider('sfBuilder', ['sfPathProvider', function(s
266273

267274
},
268275
builder: builders,
276+
stdBuilders: stdBuilders,
269277
internalBuild: build
270278
};
271279
}];

0 commit comments

Comments
 (0)