Skip to content

Commit 89f4ac8

Browse files
committed
fieldId()'s for names and ids
1 parent e29b688 commit 89f4ac8

File tree

9 files changed

+113
-30
lines changed

9 files changed

+113
-30
lines changed

dist/bootstrap-decorator.js

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

dist/schema-form.js

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,10 @@ angular.module('schemaForm').provider('schemaFormDecorators',
463463
replace: false,
464464
transclude: false,
465465
scope: true,
466-
require: '?^sfSchema',
467-
link: function(scope, element, attrs, sfSchema) {
466+
require: ['?^sfSchema', '?^form'],
467+
link: function(scope, element, attrs, Ctrl) {
468+
var sfSchema = Ctrl[0];
469+
var formCtrl = Ctrl[1];
468470

469471
//The ngModelController is used in some templates and
470472
//is needed for error messages,
@@ -589,6 +591,19 @@ angular.module('schemaForm').provider('schemaFormDecorators',
589591
);
590592
};
591593

594+
scope.fieldId = function(prependFormName, omitNumbers) {
595+
if(scope.form.key){
596+
var fieldKey = scope.form.key;
597+
if(omitNumbers){
598+
fieldKey = fieldKey.filter(function(key){
599+
return !angular.isNumber(key);
600+
});
601+
}
602+
return ((prependFormName && formCtrl && formCtrl.$name)?formCtrl.$name+'-':'')+fieldKey.join('-');
603+
}
604+
return '';
605+
};
606+
592607
// Rebind our part of the form to the scope.
593608
var once = scope.$watch(attrs.form, function(form) {
594609
if (form) {
@@ -598,6 +613,10 @@ angular.module('schemaForm').provider('schemaFormDecorators',
598613
form.ngModelOptions = form.ngModelOptions || {};
599614
scope.form = form;
600615

616+
// append the field-id to the htmlClass
617+
if(!scope.form.htmlClass){ scope.form.htmlClass = ''; }
618+
scope.form.htmlClass += (scope.form.htmlClass?' ':'')+scope.fieldId(false, true);
619+
601620
//ok let's replace that template!
602621
//We do this manually since we need to bind ng-model properly and also
603622
//for fieldsets to recurse properly.
@@ -1928,9 +1947,12 @@ angular.module('schemaForm').directive('sfField',
19281947
replace: false,
19291948
transclude: false,
19301949
scope: true,
1931-
require: '^sfSchema',
1950+
require: ['^sfSchema', '?^form'],
19321951
link: {
1933-
pre: function(scope, element, attrs, sfSchema) {
1952+
pre: function(scope, element, attrs, Ctrl) {
1953+
var sfSchema = Ctrl[0];
1954+
var formCtrl = Ctrl[1];
1955+
19341956
//The ngModelController is used in some templates and
19351957
//is needed for error messages,
19361958
scope.$on('schemaFormPropagateNgModelController', function(event, ngModel) {
@@ -1942,12 +1964,16 @@ angular.module('schemaForm').directive('sfField',
19421964
// Fetch our form.
19431965
scope.form = sfSchema.lookup['f' + attrs.sfField];
19441966
},
1945-
post: function(scope, element, attrs, sfSchema) {
1967+
post: function(scope, element, attrs, Ctrl) {
1968+
var sfSchema = Ctrl[0];
1969+
var formCtrl = Ctrl[1];
1970+
19461971
//Keep error prone logic from the template
19471972
scope.showTitle = function() {
19481973
return scope.form && scope.form.notitle !== true && scope.form.title;
19491974
};
19501975

1976+
19511977
scope.listToCheckboxValues = function(list) {
19521978
var values = {};
19531979
angular.forEach(list, function(v) {
@@ -2073,6 +2099,23 @@ angular.module('schemaForm').directive('sfField',
20732099
);
20742100
};
20752101

2102+
scope.fieldId = function(prependFormName, omitNumbers) {
2103+
if(scope.form.key){
2104+
var fieldKey = scope.form.key;
2105+
if(omitNumbers){
2106+
fieldKey = fieldKey.filter(function(key){
2107+
return !angular.isNumber(key);
2108+
});
2109+
}
2110+
return ((prependFormName && formCtrl && formCtrl.$name)?formCtrl.$name+'-':'')+fieldKey.join('-');
2111+
}
2112+
return '';
2113+
};
2114+
2115+
// append the field-id to the htmlClass
2116+
if(!scope.form.htmlClass){ scope.form.htmlClass = ''; }
2117+
scope.form.htmlClass += (scope.form.htmlClass?' ':'')+scope.fieldId(false, true);
2118+
20762119
var form = scope.form;
20772120

20782121
// Where there is a key there is probably a ngModel

src/directives/decorators/bootstrap/checkbox.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
ng-model-options="form.ngModelOptions"
99
schema-validate="form"
1010
class="{{form.fieldHtmlClass}}"
11-
name="{{form.key.slice(-1)[0]}}">
11+
name="{{fieldId()}}">
1212
<span ng-bind-html="form.title"></span>
1313
</label>
1414
<div class="help-block" sf-message="form.description"></div>

src/directives/decorators/bootstrap/checkboxes.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
sf-changed="form"
1010
class="{{form.fieldHtmlClass}}"
1111
ng-model="titleMapValues[$index]"
12-
name="{{form.key.slice(-1)[0]}}">
12+
name="{{fieldId()}}">
1313
<span ng-bind-html="form.titleMap[$index].name"></span>
1414
</label>
1515

src/directives/decorators/bootstrap/default.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div class="form-group schema-form-{{form.type}} {{form.htmlClass}}"
22
ng-class="{'has-error': form.disableErrorState !== true && hasError(), 'has-success': form.disableSuccessState !== true && hasSuccess(), 'has-feedback': form.feedback !== false }">
3-
<label class="control-label {{form.labelHtmlClass}}" ng-class="{'sr-only': !showTitle()}" for="{{form.key.slice(-1)[0]}}">{{form.title}}</label>
3+
<label class="control-label {{form.labelHtmlClass}}" ng-class="{'sr-only': !showTitle()}" for="{{fieldId(true)}}">{{form.title}}</label>
44

55
<input ng-if="!form.fieldAddonLeft && !form.fieldAddonRight"
66
ng-show="form.key"
@@ -9,13 +9,13 @@
99
sf-changed="form"
1010
placeholder="{{form.placeholder}}"
1111
class="form-control {{form.fieldHtmlClass}}"
12-
id="{{form.key.slice(-1)[0]}}"
12+
id="{{fieldId(true)}}"
1313
ng-model-options="form.ngModelOptions"
1414
ng-model="$$value$$"
1515
ng-disabled="form.readonly"
1616
schema-validate="form"
17-
name="{{form.key.slice(-1)[0]}}"
18-
aria-describedby="{{form.key.slice(-1)[0] + 'Status'}}">
17+
name="{{fieldId()}}"
18+
aria-describedby="{{fieldId(true) + 'Status'}}">
1919

2020
<div ng-if="form.fieldAddonLeft || form.fieldAddonRight"
2121
ng-class="{'input-group': (form.fieldAddonLeft || form.fieldAddonRight)}">
@@ -28,13 +28,13 @@
2828
sf-changed="form"
2929
placeholder="{{form.placeholder}}"
3030
class="form-control {{form.fieldHtmlClass}}"
31-
id="{{form.key.slice(-1)[0]}}"
31+
id="{{fieldId(true)}}"
3232
ng-model-options="form.ngModelOptions"
3333
ng-model="$$value$$"
3434
ng-disabled="form.readonly"
3535
schema-validate="form"
36-
name="{{form.key.slice(-1)[0]}}"
37-
aria-describedby="{{form.key.slice(-1)[0] + 'Status'}}">
36+
name="{{fieldId()}}"
37+
aria-describedby="{{fieldId(true) + 'Status'}}">
3838

3939
<span ng-if="form.fieldAddonRight"
4040
class="input-group-addon"
@@ -47,7 +47,7 @@
4747
aria-hidden="true"></span>
4848

4949
<span ng-if="hasError() || hasSuccess()"
50-
id="{{form.key.slice(-1)[0] + 'Status'}}"
50+
id="{{fieldId(true) + 'Status'}}"
5151
class="sr-only">{{ hasSuccess() ? '(success)' : '(error)' }}</span>
5252

5353
<div class="help-block" sf-message="form.description"></div>

src/directives/decorators/bootstrap/select.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class="form-control {{form.fieldHtmlClass}}"
1111
schema-validate="form"
1212
ng-options="item.value as item.name group by item.group for item in form.titleMap"
13-
name="{{form.key.slice(-1)[0]}}">
13+
name="{{fieldId()}}">
1414
</select>
1515
<div class="help-block" sf-message="form.description"></div>
1616
</div>

src/directives/decorators/bootstrap/textarea.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
<div class="form-group has-feedback {{form.htmlClass}} schema-form-textarea" ng-class="{'has-error': form.disableErrorState !== true && hasError(), 'has-success': form.disableSuccessState !== true && hasSuccess()}">
2-
<label class="{{form.labelHtmlClass}}" ng-class="{'sr-only': !showTitle()}" for="{{form.key.slice(-1)[0]}}">{{form.title}}</label>
2+
<label class="{{form.labelHtmlClass}}" ng-class="{'sr-only': !showTitle()}" for="{{fieldId(true)}}">{{form.title}}</label>
33

44
<textarea ng-if="!form.fieldAddonLeft && !form.fieldAddonRight"
55
class="form-control {{form.fieldHtmlClass}}"
6-
id="{{form.key.slice(-1)[0]}}"
6+
id="{{fieldId(true)}}"
77
sf-changed="form"
88
placeholder="{{form.placeholder}}"
99
ng-disabled="form.readonly"
1010
ng-model="$$value$$"
1111
ng-model-options="form.ngModelOptions"
1212
schema-validate="form"
13-
name="{{form.key.slice(-1)[0]}}"></textarea>
13+
name="{{fieldId()}}"></textarea>
1414

1515
<div ng-if="form.fieldAddonLeft || form.fieldAddonRight"
1616
ng-class="{'input-group': (form.fieldAddonLeft || form.fieldAddonRight)}">
1717
<span ng-if="form.fieldAddonLeft"
1818
class="input-group-addon"
1919
ng-bind-html="form.fieldAddonLeft"></span>
2020
<textarea class="form-control {{form.fieldHtmlClass}}"
21-
id="{{form.key.slice(-1)[0]}}"
21+
id="{{fieldId(true)}}"
2222
sf-changed="form"
2323
placeholder="{{form.placeholder}}"
2424
ng-disabled="form.readonly"
2525
ng-model="$$value$$"
2626
ng-model-options="form.ngModelOptions"
2727
schema-validate="form"
28-
name="{{form.key.slice(-1)[0]}}"></textarea>
28+
name="{{fieldId()}}"></textarea>
2929
<span ng-if="form.fieldAddonRight"
3030
class="input-group-addon"
3131
ng-bind-html="form.fieldAddonRight"></span>

src/directives/field.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ angular.module('schemaForm').directive('sfField',
99
replace: false,
1010
transclude: false,
1111
scope: true,
12-
require: '^sfSchema',
12+
require: ['^sfSchema', '?^form'],
1313
link: {
14-
pre: function(scope, element, attrs, sfSchema) {
14+
pre: function(scope, element, attrs, Ctrl) {
15+
var sfSchema = Ctrl[0];
16+
var formCtrl = Ctrl[1];
17+
1518
//The ngModelController is used in some templates and
1619
//is needed for error messages,
1720
scope.$on('schemaFormPropagateNgModelController', function(event, ngModel) {
@@ -154,6 +157,23 @@ angular.module('schemaForm').directive('sfField',
154157
);
155158
};
156159

160+
scope.fieldId = function(prependFormName, omitNumbers) {
161+
if(scope.form.key){
162+
var fieldKey = scope.form.key;
163+
if(omitNumbers){
164+
fieldKey = fieldKey.filter(function(key){
165+
return !angular.isNumber(key);
166+
});
167+
}
168+
return ((prependFormName && formCtrl && formCtrl.$name)?formCtrl.$name+'-':'')+fieldKey.join('-');
169+
}
170+
return '';
171+
};
172+
173+
// append the field-id to the htmlClass
174+
if(!scope.form.htmlClass){ scope.form.htmlClass = ''; }
175+
scope.form.htmlClass += (scope.form.htmlClass?' ':'')+scope.fieldId(false, true);
176+
157177
var form = scope.form;
158178

159179
// Where there is a key there is probably a ngModel

src/services/decorators.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ angular.module('schemaForm').provider('schemaFormDecorators',
3939
replace: false,
4040
transclude: false,
4141
scope: true,
42-
require: '?^sfSchema',
43-
link: function(scope, element, attrs, sfSchema) {
42+
require: ['?^sfSchema', '?^form'],
43+
link: function(scope, element, attrs, Ctrl) {
44+
var sfSchema = Ctrl[0];
45+
var formCtrl = Ctrl[1];
4446

4547
//The ngModelController is used in some templates and
4648
//is needed for error messages,
@@ -149,6 +151,20 @@ angular.module('schemaForm').provider('schemaFormDecorators',
149151
return scope.ngModel.$invalid && !scope.ngModel.$pristine;
150152
};
151153

154+
scope.fieldId = function(prependFormName, omitNumbers) {
155+
if(scope.form.key){
156+
var fieldKey = scope.form.key;
157+
if(omitNumbers){
158+
fieldKey = fieldKey.filter(function(key){
159+
return !angular.isNumber(key);
160+
});
161+
}
162+
return ((prependFormName && formCtrl && formCtrl.$name)?formCtrl.$name+'-':'')+fieldKey.join('-');
163+
}
164+
return '';
165+
};
166+
167+
152168
/**
153169
* DEPRECATED: use sf-messages instead.
154170
* Error message handler
@@ -174,6 +190,10 @@ angular.module('schemaForm').provider('schemaFormDecorators',
174190
form.ngModelOptions = form.ngModelOptions || {};
175191
scope.form = form;
176192

193+
// append the field-id to the htmlClass
194+
if(!scope.form.htmlClass){ scope.form.htmlClass = ''; }
195+
scope.form.htmlClass += (scope.form.htmlClass?' ':'')+scope.fieldId(false, true);
196+
177197
//ok let's replace that template!
178198
//We do this manually since we need to bind ng-model properly and also
179199
//for fieldsets to recurse properly.

0 commit comments

Comments
 (0)