Skip to content

Commit 8a89d28

Browse files
committed
checkboxlist required: work in progress
1 parent 499ba7c commit 8a89d28

File tree

6 files changed

+119
-18
lines changed

6 files changed

+119
-18
lines changed

src/angular-form-gen/angular-form-gen-templates.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.

src/angular-form-gen/angular-form-gen.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ fg.config(function ($provide) {
122122
pattern: function (nameOrObject, pattern) {
123123

124124
if (angular.isString(nameOrObject)) {
125-
config.validation.patterns[name] = pattern;
125+
config.validation.patterns[nameOrObject] = pattern;
126126
} else {
127127
angular.extend(config.validation.patterns, nameOrObject);
128128
}
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
<div class="checkbox" ng-repeat="option in field.schema.options">
1+
<div class="checkbox" ng-repeat="option in field.schema.options" ng-model="form.data[field.schema.name]"
2+
fg-required-checkboxlist>
23
<label title="{{ field.schema.tooltip }}">
3-
<input fg-field-input
4-
type="checkbox"
5-
tabindex="{{ tabIndex }}"
6-
name="{{ field.schema.name }}[]" value="{{ option.value }}"
7-
ng-model="form.data[field.schema.name][option.value]">
8-
<span>{{option.text || option.value}}</span>
4+
<input fg-field-input
5+
type="checkbox"
6+
tabindex="{{ tabIndex }}"
7+
name="{{ field.schema.name }}[]" value="{{ option.value }}"
8+
ng-model="form.data[field.schema.name][option.value]">
9+
<span>{{option.text || option.value}}</span>
910
</label>
1011
</div>

src/angular-form-gen/field-templates/properties/checkboxlist.ng.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
<div fg-tabs-pane="Options">
55
<div fg-property-field-options="multiple"></div>
66
</div>
7+
<div fg-tabs-pane="Validation">
8+
<div fg-property-field-validation="{ required: true }"></div>
9+
</div>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
fg.directive('fgRequiredCheckboxlist', function() {
2+
3+
function validate(required, value, options) {
4+
5+
// Set in field-templates/default/checkboxlist.ng.html
6+
7+
if(required) {
8+
9+
// Ensures that at least one option is checked
10+
11+
var x = options.length;
12+
13+
while(x--) {
14+
if(value[options[x].value]) {
15+
return false;
16+
}
17+
}
18+
19+
return true;
20+
}
21+
22+
}
23+
24+
var validationName = 'requiredCheckboxlist';
25+
26+
return {
27+
require: ['ngModel', '^fgField'],
28+
link: function($scope, $element, $attrs, $ctrls) {
29+
30+
var ngModel = $ctrls[0], schema = $scope.field.schema;
31+
32+
$scope.$watchCollection('form.data[field.schema.name]', function(value) {
33+
ngModel.$setValidity(validationName, validate(schema.validation.required, value, schema.options));
34+
});
35+
36+
//ngModel.$parsers.unshift(function(viewValue) {
37+
//
38+
//
39+
// form.state.$setValidity(validationName, validate(schema.validation.required, form.data[schema.name], schema.options));
40+
//
41+
// return viewValue;
42+
//
43+
//});
44+
45+
}
46+
};
47+
});

src/app/demo/form-resource.js

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,42 @@ app.factory('Form', function (fakeHttpResolve, formData) {
7070
app.factory('formData', function () {
7171
var forms = [
7272
{
73-
74-
"name": "Radiobutton list",
73+
"name": "Required option fields",
7574
"layout": "form-horizontal",
75+
"description": "An example schema containing required option fields.",
7676
"schema": {
7777
"fields": [
78+
{
79+
"type": "checkboxlist",
80+
"name": "checkboxes",
81+
"displayName": "Checkboxes",
82+
"options": [
83+
{
84+
"value": "1",
85+
"text": "Option 1"
86+
},
87+
{
88+
"value": "2",
89+
"text": "Option 2"
90+
},
91+
{
92+
"value": "3",
93+
"text": "Option 3"
94+
}
95+
],
96+
"value": {
97+
"1": false,
98+
"2": false
99+
},
100+
"validation": {
101+
"messages": {},
102+
"required": true
103+
}
104+
},
78105
{
79106
"type": "radiobuttonlist",
80-
"name": "field6145",
81-
"displayName": "Radiobutton List",
107+
"name": "radiobuttons",
108+
"displayName": "Radiobuttons",
82109
"options": [
83110
{
84111
"value": "1",
@@ -93,14 +120,37 @@ app.factory('formData', function () {
93120
"text": "Option 3"
94121
}
95122
],
96-
"value": "1"
123+
"validation": {
124+
"messages": {},
125+
"required": true
126+
}
127+
},
128+
{
129+
"type": "selectlist",
130+
"name": "select",
131+
"displayName": "Select",
132+
"options": [
133+
{
134+
"value": "1",
135+
"text": "Option 1"
136+
},
137+
{
138+
"value": "2",
139+
"text": "Option 2"
140+
},
141+
{
142+
"value": "3",
143+
"text": "Option 3"
144+
}
145+
],
146+
"validation": {
147+
"messages": {},
148+
"required": true
149+
}
97150
}
98151
]
99152
}
100-
101-
102153
},
103-
104154
{
105155
"schema": {
106156
"name": "All Fields",

0 commit comments

Comments
 (0)