Skip to content

Commit 86ab686

Browse files
committed
Merge branch 'feature/lotsofstuff' into development
2 parents 4f7c69a + ea7f7d7 commit 86ab686

File tree

14 files changed

+338
-10
lines changed

14 files changed

+338
-10
lines changed

problem.txt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
3+
4+
Olika vägar frammåt:
5+
====================
6+
7+
8+
9+
Problemställningen:
10+
-------------------
11+
json-form fixar en hel del validering, dels genom att läggapå attribut som
12+
min och max på input och dels genom att validera via en schema validtor.
13+
14+
Jag tänkte undvika validatorn och köra "the angular way" genom att ha all validering
15+
i formuläret via HTML5 valideringar och directives som ng-pattern, ng-maxlength osv
16+
antagligen ett par egna.
17+
18+
Hittils har algoritmen sett ut såhär:
19+
#1 schema => defaultForm,
20+
#2 defaultForm+form => finalForm
21+
#3 loopa finalForm och skapa html, dvs all info för validering i formuläret.
22+
23+
Kruxet är att formulär definitionen i json form *inte* innehåller tillräckligt med valideringar
24+
(den innehåller några) utan jag måste lägga till form attribut för dem. (json form tar dem från
25+
schemat)
26+
27+
tex så om schema innehåller "maxLength" attribut så sätts "maxlength" på input fältet, men om form
28+
definitionen innehåller "maxLength" (eller "maxlength" för den delen) så händer inget.
29+
30+
I min algoritm ovan där form definitionen måste innehålla all info för generering av html och
31+
de attribut som styr upp validering så måste jag helt enkelt hitta på eget.
32+
33+
Frågan är hur ska detta egna se ut?
34+
35+
36+
#1 Egna form attribut
37+
När det inte finns ngt form attribut i json forms defintion så skapar jag ett eget, t.ex. så skulle
38+
maxLength bara kopieras till form objektet.
39+
+ minsta motståndets lag
40+
- kluddig lösning, motverkar lite att vara json form kompatibel
41+
- kan allt stödjas?
42+
43+
44+
#2 Eget schema directive
45+
Istället skapa ett eget directive som antingen validerar själv eller lägger till de attribut som
46+
behövs för att validera. Schemat fås genom require och ng-model värdet pekar ut vart i schemat.
47+
Kan möjligtvis implementeras med tv4.
48+
+ snygg lösning
49+
- krångligt directive, måste ta hänsyn till om tex maximum, required osv redan satts via form
50+
definitionen.
51+
52+
53+
54+

src/bootstrap-example.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ <h3>Model</h3>
5656
"title": "Name",
5757
"description": "Gimme yea name lad",
5858
"type": "string",
59+
"pattern": "^[^/]*$",
5960
"minLength": 2
6061
},
6162
"favorite": {
@@ -69,7 +70,8 @@ <h3>Model</h3>
6970
},
7071
"shoesize": {
7172
"title": "Shoe size",
72-
"type": "number"
73+
"default": 42,
74+
"type": "number",
7375
},
7476
"attributes": {
7577
"type": "object",
@@ -88,6 +90,16 @@ <h3>Model</h3>
8890
}
8991
}
9092
},
93+
"things": {
94+
"type": "array",
95+
"title": "I like...",
96+
"items": {
97+
"type": "string",
98+
"enum": [
99+
"clowns","compiling","sleeping"
100+
]
101+
}
102+
},
91103
"soul": {
92104
"title": "Terms Of Service",
93105
"description": "I agree to sell my undying soul",
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<div class="btn-group">
2+
<input ng-repeat-start="item in form.items"
3+
type="submit"
4+
class="btn btn-primary"
5+
value="item.title"
6+
ng-if="item.type === 'submit'">
7+
<button ng-repeat-end class="btn btn-default" ng-if="item.type !== 'submit'">{{item.title}}</button>
8+
</div>

src/directives/decorators/bootstrap/bootstrap-decorator.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,21 @@ function($parse, $compile, $http, $templateCache){
1313
if (form.type === 'fieldset') {
1414
return 'directives/decorators/bootstrap/fieldset.html';
1515
}
16+
if (form.type === 'section') {
17+
return 'directives/decorators/bootstrap/section.html';
18+
}
19+
if (form.type === 'actions') {
20+
return 'directives/decorators/bootstrap/actions.html';
21+
}
1622
if (form.type === 'select') {
1723
return 'directives/decorators/bootstrap/select.html';
1824
}
1925
if (form.type === 'checkbox') {
2026
return 'directives/decorators/bootstrap/checkbox.html';
2127
}
28+
if (form.type === 'checkboxes') {
29+
return 'directives/decorators/bootstrap/checkboxes.html';
30+
}
2231
if (form.type === 'number') {
2332
return 'directives/decorators/bootstrap/default.html';
2433
}
@@ -46,15 +55,29 @@ function($parse, $compile, $http, $templateCache){
4655
//We do this manually since we need to bind ng-model properly and also
4756
//for fieldsets to recurse properly.
4857
$http.get(templateUrl(form),{ cache: $templateCache }).then(function(res){
49-
var template = res.data.replace('$$value$$','model.'+form.key);
58+
var template = res.data.replace(/\$\$value\$\$/g,'model.'+form.key);
5059
$compile(template)(scope,function(clone){
5160
element.replaceWith(clone);
5261
});
5362
});
5463
once();
5564
});
56-
}
5765

66+
//Keep error prone logic from the template
67+
scope.showTitle = function() {
68+
return scope.form && scope.form.notitle !== true && scope.form.title;
69+
};
70+
71+
scope.checkboxValuesToList = function(values){
72+
var lst = [];
73+
angular.forEach(values,function(v,k){
74+
if (v) {
75+
lst.push(k);
76+
}
77+
});
78+
return lst;
79+
};
80+
}
5881
};
5982
}]);
6083

src/directives/decorators/bootstrap/checkbox.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="checkbox" ng-class="{'has-error': hasError()}">
2-
<label ng-show="form.title">
2+
<label>
33
<input type="checkbox" ng-model="$$value$$" schema-validate="form.schema" ng-required="form.required">
44
{{form.title}}
55
</label>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<div class="form-group" ng-class="{'has-error': hasError()}" ng-init="checkboxValues = {}">
2+
<label ng-show="showTitle()">{{form.title}}</label>
3+
<div class="checkbox" ng-repeat="(value,name) in form.titleMap" >
4+
<label>
5+
<input type="checkbox" ng-model="checkboxValues[value]" ng-change="$$value$$ = checkboxValuesToList(checkboxValues)" >
6+
{{name}}
7+
</label>
8+
</div>
9+
<span class="help-block" ng-show="form.description">{{form.description}}</span>
10+
</div>

src/directives/decorators/bootstrap/default.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
<div class="form-group" ng-class="{'has-error': hasError()}">
2-
<label ng-show="form.title">{{form.title}}</label>
2+
<label ng-show="showTitle()">{{form.title}}</label>
33

4-
<input type="{{form.type}}" class="form-control" ng-required="form.required" ng-model="$$value$$" schema-validate="form.schema">
4+
<input type="{{form.type}}"
5+
placeholder="{{form.placeholder}}"
6+
class="form-control"
7+
ng-required="form.required"
8+
ng-model="$$value$$"
9+
schema-validate="form.schema">
510

611
<span class="help-block" ng-show="form.description && !hasError()">{{form.description}} </span>
712
<span class="help-block" ng-show="hasError()">{{schemaError}} {{ngModel.$error}}</span>

src/directives/decorators/bootstrap/readonly.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="form-group">
2-
<label ng-show="form.title">{{form.title}}</label>
2+
<label ng-show="showTitle()">{{form.title}}</label>
33
<input ng-if="form.type !== 'textarea'" type="text" disabled class="form-control" value="{{$$value$$}}">
44
<textarea ng-if="form.type === 'textarea'" disabled class="form-control">{{$$value$$}}</textarea>
55
<span class="help-block" ng-show="form.description">{{form.description}} </span>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
<bootstrap-decorator ng-repeat="item in form.items" form="item"></bootstrap-decorator>
3+
</div>

src/directives/decorators/bootstrap/select.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="form-group" ng-class="{'has-error': hasError()}">
2-
<label ng-show="form.title">
2+
<label ng-show="showTitle()">
33
{{form.title}}
44
</label>
55
<select ng-model="$$value$$"

0 commit comments

Comments
 (0)