Skip to content

fieldId()'s for names and ids #560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/directives/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ angular.module('schemaForm').directive('sfField',
replace: false,
transclude: false,
scope: true,
require: '^sfSchema',
require: ['^sfSchema', '?^form'],
link: {
pre: function(scope, element, attrs, sfSchema) {
pre: function(scope, element, attrs, Ctrl) {
var sfSchema = Ctrl[0];
var formCtrl = Ctrl[1];

//The ngModelController is used in some templates and
//is needed for error messages,
scope.$on('schemaFormPropagateNgModelController', function(event, ngModel) {
Expand All @@ -23,7 +26,10 @@ angular.module('schemaForm').directive('sfField',
// Fetch our form.
scope.form = sfSchema.lookup['f' + attrs.sfField];
},
post: function(scope, element, attrs, sfSchema) {
post: function(scope, element, attrs, Ctrl) {
var sfSchema = Ctrl[0];
var formCtrl = Ctrl[1];

//Keep error prone logic from the template
scope.showTitle = function() {
return scope.form && scope.form.notitle !== true && scope.form.title;
Expand Down Expand Up @@ -154,6 +160,23 @@ angular.module('schemaForm').directive('sfField',
);
};

scope.fieldId = function(prependFormName, omitNumbers) {
if(scope.form.key){
var fieldKey = scope.form.key;
if(omitNumbers){
fieldKey = fieldKey.filter(function(key){
return !angular.isNumber(key);
});
}
return ((prependFormName && formCtrl && formCtrl.$name)?formCtrl.$name+'-':'')+fieldKey.join('-');
}
return '';
};

// append the field-id to the htmlClass
if(!scope.form.htmlClass){ scope.form.htmlClass = ''; }
scope.form.htmlClass += (scope.form.htmlClass?' ':'')+scope.fieldId(false, true);

var form = scope.form;

// Where there is a key there is probably a ngModel
Expand Down
23 changes: 21 additions & 2 deletions src/services/decorators.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ angular.module('schemaForm').provider('schemaFormDecorators',
replace: false,
transclude: false,
scope: true,
require: '?^sfSchema',
link: function(scope, element, attrs, sfSchema) {
require: ['?^sfSchema', '?^form'],
link: function(scope, element, attrs, Ctrl) {
var sfSchema = Ctrl[0];
var formCtrl = Ctrl[1];

//The ngModelController is used in some templates and
//is needed for error messages,
Expand Down Expand Up @@ -149,6 +151,19 @@ angular.module('schemaForm').provider('schemaFormDecorators',
return scope.ngModel.$invalid && !scope.ngModel.$pristine;
};

scope.fieldId = function(prependFormName, omitNumbers) {
if(scope.form.key){
var fieldKey = scope.form.key;
if(omitNumbers){
fieldKey = fieldKey.filter(function(key){
return !angular.isNumber(key);
});
}
return ((prependFormName && formCtrl && formCtrl.$name)?formCtrl.$name+'-':'')+fieldKey.join('-');
}
return '';
};

/**
* DEPRECATED: use sf-messages instead.
* Error message handler
Expand All @@ -174,6 +189,10 @@ angular.module('schemaForm').provider('schemaFormDecorators',
form.ngModelOptions = form.ngModelOptions || {};
scope.form = form;

// append the field-id to the htmlClass
if(!scope.form.htmlClass){ scope.form.htmlClass = ''; }
scope.form.htmlClass += (scope.form.htmlClass?' ':'')+scope.fieldId(false, true);

//ok let's replace that template!
//We do this manually since we need to bind ng-model properly and also
//for fieldsets to recurse properly.
Expand Down