Skip to content
This repository was archived by the owner on Apr 30, 2018. It is now read-only.

Commit 6d8da6b

Browse files
author
Kent C. Dodds
committed
Adding submitButtonTemplate to customize the submit button.
1 parent d8d8180 commit 6d8da6b

File tree

6 files changed

+20
-5
lines changed

6 files changed

+20
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Version numbers correspond to `bower.json` version
66

77
Adding the `formlyOptionsProvider` which allows developers to set default form options.
88

9+
Adding `submitButtonTemplate` to the configurable `formlyOptions`. If it has a value, then the button in the `formly-form` directive will be replaced with the compiled (on the scope) version of that value. Also adding twitter classes to the submit button so hopefully this template will be unecessary in some cases.
10+
911
## Bug Fixes
1012

1113
## Breaking Changes

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ Forms can be customized with the options below. Note, you can configure this on
9292
#### submitCopy (string, optional)
9393
>`submitCopy` customizes the submit button copy. Defaults to 'Submit'.
9494
95+
#### submitButtonTemplate (string, optional)
96+
>`submitButtonTemplate` customizes the template used for the submit button. Compiled on the scope, so you have access to all other options (and any custom options) in your custom template.
97+
9598
### Creating Form Fields
9699
When constructing fields use the options below to customize each field object. You must set at least a `type`, `template`, or `templateUrl`.
97100

src/app.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ app.config(function($stateProvider, $urlRouterProvider, $locationProvider, forml
3333
// or
3434
formlyOptionsProvider.setOption({
3535
submitCopy: 'Configured Submit',
36-
hideSubmit: true
36+
hideSubmit: true,
37+
submitButtonTemplate: [
38+
'<button type="submit" class="btn btn-primary" ng-hide="options.hideSubmit">',
39+
'{{options.submitCopy || "Submit"}} boo yeah!',
40+
'</button>'
41+
].join('')
3742
});
3843
});
3944

src/directives/formly-form.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
index="$index"
88
ng-hide="field.hide">
99
</formly-field>
10-
<button type="submit" class="btn btn-primary"
10+
<button type="submit"
11+
class="btn btn-primary"
1112
ng-hide="options.hideSubmit">
1213
{{options.submitCopy || "Submit"}}
1314
</button>

src/directives/formly-form.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
angular.module('formly.render')
3-
.directive('formlyForm', function formlyForm(formlyOptions) {
3+
.directive('formlyForm', function formlyForm(formlyOptions, $compile) {
44
return {
55
restrict: 'AE',
66
templateUrl: 'directives/formly-form.html',
@@ -14,14 +14,17 @@ angular.module('formly.render')
1414
compile: function (scope, iElement, iAttrs, controller, transcludeFn) {
1515
return {
1616
post: function (scope, ele, attr, controller) {
17+
scope.options = angular.extend(formlyOptions.getOptions(), scope.options);
18+
if (scope.options.submitButtonTemplate) {
19+
ele.find('button').replaceWith($compile(scope.options.submitButtonTemplate)(scope));
20+
}
1721
//Post gets called after angular has created the FormController
1822
//Now pass the FormController back up to the parent scope
1923
scope.formOnParentScope = scope[attr.name];
2024
}
2125
}
2226
},
2327
controller: function($scope, $element, $parse) {
24-
$scope.options = angular.extend(formlyOptions.getOptions(), $scope.options);
2528
$scope.$watch('result', function(newValue) {
2629
angular.forEach($scope.fields, function(field, index) {
2730
if (field.hideExpression) {

src/providers/formly-options.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ angular.module('formly.render')
55
var options = {
66
uniqueFormId: null,
77
submitCopy: "Submit",
8-
hideSubmit: false
8+
hideSubmit: false,
9+
submitButtonTemplate: null
910
};
1011

1112
function setOption(name, value) {

0 commit comments

Comments
 (0)