Skip to content

Commit 5a63e1b

Browse files
author
Null McNull
committed
checkboxlist validation
1 parent 8a89d28 commit 5a63e1b

File tree

10 files changed

+157
-69
lines changed

10 files changed

+157
-69
lines changed

.idea/jsLibraryMappings.xml

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

.idea/runConfigurations/gulpfile_js.xml

Lines changed: 2 additions & 0 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-templates.js

Lines changed: 3 additions & 3 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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,10 @@ fg.config(function (fgConfigProvider, FgField) {
151151
unique: 'The value "{{ field.state.$viewValue }}" is already in use.',
152152
number: 'The value "{{ field.state.$viewValue }}" is not a number.',
153153
min: 'The value {{ field.schema && ("should be at least " + field.schema.validation.min) || field.state.$viewValue + " is too low" }}',
154-
max: 'The value {{ field.schema && ("should be less than " + field.schema.validation.max) || field.state.$viewValue + " is too high" }}'
154+
max: 'The value {{ field.schema && ("should be less than " + field.schema.validation.max) || field.state.$viewValue + " is too high" }}',
155+
requiredOption: 'Please select an option.',
156+
minoptions: 'At least {{ field.schema.validation.minoptions }} option(s) should be selected.',
157+
maxoptions: 'No more than {{ field.schema.validation.maxoptions }} option(s) should be selected.'
155158
});
156159

157160
// - - - - - - - - - - - - - - - - - - - - - -

src/angular-form-gen/edit/canvas/field/field.ng.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
</button>
3535
</div>
3636
</div>
37-
<div ng-form fg-form-required-filter>
37+
<div ng-form fg-null-form>
3838
<div fg-field="field"
3939
fg-tab-index="-1"
4040
fg-edit-mode="true"
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
<div class="checkbox" ng-repeat="option in field.schema.options" ng-model="form.data[field.schema.name]"
2-
fg-required-checkboxlist>
3-
<label title="{{ field.schema.tooltip }}">
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>
10-
</label>
11-
</div>
1+
<div fg-checkboxlist fg-field-input ng-model="form.data[field.schema.name]" name="{{ field.schema.name }}">
2+
<div class="checkbox" ng-repeat="option in field.schema.options">
3+
<label title="{{ field.schema.tooltip }}">
4+
<input type="checkbox"
5+
tabindex="{{ tabIndex }}"
6+
value="{{ option.value }}"
7+
ng-model="form.data[field.schema.name][option.value]">
8+
<span>{{option.text || option.value}}</span>
9+
</label>
10+
</div>
11+
</div>

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,40 @@
66
</div>
77
<div fg-tabs-pane="Validation">
88
<div fg-property-field-validation="{ required: true }"></div>
9+
10+
<div class="fg-property-field-validation">
11+
<div fg-property-field="minoptions"
12+
fg-property-field-label="Minimum options">
13+
<input type="text"
14+
fg-field-redraw
15+
fg-input-number
16+
title="The minimum number of options that should be selected."
17+
name="minoptions"
18+
ng-model="field.validation.minoptions"
19+
class="form-control"/>
20+
</div>
21+
22+
<div ng-if="field.validation.minoptions >= 1" >
23+
<div fg-edit-validation-message="minoptions"></div>
24+
</div>
25+
</div>
26+
27+
<div class="fg-property-field-validation">
28+
<div fg-property-field="maxoptions"
29+
fg-property-field-label="Maximum options">
30+
<input type="text"
31+
fg-field-redraw
32+
fg-input-number
33+
title="The maximum number of options that can be selected."
34+
name="maxoptions"
35+
ng-model="field.validation.maxoptions"
36+
class="form-control"/>
37+
</div>
38+
39+
<div ng-if="field.validation.maxoptions >= 1" >
40+
<div fg-edit-validation-message="minoptions"></div>
41+
</div>
42+
</div>
43+
44+
945
</div>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
fg.directive('fgCheckboxlist', function() {
2+
3+
function validateRequired(validation, value, options) {
4+
5+
var required = validation ? validation.required : false;
6+
7+
// Set in field-templates/default/checkboxlist.ng.html
8+
9+
if(required) {
10+
11+
// Ensures that at least one option is checked
12+
13+
var x = options.length;
14+
15+
while(x--) {
16+
if(value[options[x].value]) {
17+
return true;
18+
}
19+
}
20+
21+
return false;
22+
}
23+
24+
return true;
25+
26+
}
27+
28+
function selectionCount(value) {
29+
var c = 0;
30+
31+
for(var k in value) {
32+
if(value[k]) {
33+
c += 1;
34+
}
35+
}
36+
37+
return c;
38+
}
39+
40+
return {
41+
require: ['^fgField'],
42+
link: function($scope, $element, $attrs, $ctrls) {
43+
44+
var field = $ctrls[0].field();
45+
46+
var formData = $scope.form.data, schema = field.schema;
47+
48+
$scope.$watchCollection(function() {
49+
return formData[schema.name];
50+
}, function(value, oldValue) {
51+
52+
// Ensure that the field is marked as dirty on changes
53+
if(!field.state.$dirty && value !== oldValue) {
54+
field.state.$setViewValue(value);
55+
}
56+
57+
if(schema.validation) {
58+
var required = validateRequired(schema.validation, value, schema.options);
59+
field.state.$setValidity('requiredOption', required);
60+
61+
var minc = schema.validation.minoptions;
62+
var maxc = schema.validation.maxoptions;
63+
64+
var min = true, max = true;
65+
66+
if(minc || maxc) {
67+
var c = selectionCount(value);
68+
69+
if(minc) {
70+
min = c >= schema.validation.minoptions;
71+
}
72+
73+
if(maxc) {
74+
max = c <= schema.validation.maxoptions;
75+
}
76+
}
77+
78+
field.state.$setValidity('minoptions', min);
79+
field.state.$setValidity('maxoptions', max);
80+
}
81+
});
82+
}
83+
};
84+
});

src/angular-form-gen/validation/required-checkboxlist.js

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/app/demo/form-resource.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ app.factory('Form', function (fakeHttpResolve, formData) {
6767

6868
});
6969

70+
7071
app.factory('formData', function () {
7172
var forms = [
7273
{
@@ -91,6 +92,14 @@ app.factory('formData', function () {
9192
{
9293
"value": "3",
9394
"text": "Option 3"
95+
},
96+
{
97+
"value": "4",
98+
"text": "Option 4"
99+
},
100+
{
101+
"value": "5",
102+
"text": "Option 5"
94103
}
95104
],
96105
"value": {
@@ -99,7 +108,9 @@ app.factory('formData', function () {
99108
},
100109
"validation": {
101110
"messages": {},
102-
"required": true
111+
"required": true,
112+
"minoptions": 2,
113+
"maxoptions": 3
103114
}
104115
},
105116
{

0 commit comments

Comments
 (0)