Checking for email type
diff --git a/views/ngXosLib/bower.json b/views/ngXosLib/bower.json
index 18ee79e..55adc5c 100644
--- a/views/ngXosLib/bower.json
+++ b/views/ngXosLib/bower.json
@@ -23,6 +23,7 @@
"lodash": "~4.11.1"
},
"devDependencies": {
- "angular-mocks": "1.4.7"
+ "angular-mocks": "1.4.7",
+ "jasmine-jquery": "~2.1.1"
}
}
diff --git a/views/ngXosLib/xosHelpers/spec/ui/form.test.js b/views/ngXosLib/xosHelpers/spec/ui/form.test.js
index 4b33a5a..5d2b833 100644
--- a/views/ngXosLib/xosHelpers/spec/ui/form.test.js
+++ b/views/ngXosLib/xosHelpers/spec/ui/form.test.js
@@ -15,6 +15,7 @@
let fields = [
'id',
'name',
+ 'mail',
'active',
'created',
'custom'
@@ -23,6 +24,7 @@
let modelField = {
id: {},
name: {},
+ mail: {},
active: {},
created: {},
custom: {}
@@ -31,6 +33,7 @@
let model = {
id: 1,
name: 'test',
+ mail: 'test@onlab.us',
active: true,
created: '2016-04-18T23:44:16.883181Z',
custom: 'MyCustomValue'
@@ -55,6 +58,11 @@
type: 'string',
validators: {}
},
+ mail: {
+ label: 'Mail:',
+ type: 'email',
+ validators: {}
+ },
active: {
label: 'Active:',
type: 'boolean',
@@ -81,10 +89,23 @@
service = _XosFormHelpers_;
}));
+ describe('the _isEmail method', () => {
+ it('should return true', () => {
+ expect(service._isEmail('test@onlab.us')).toEqual(true);
+ });
+ it('should return false', () => {
+ expect(service._isEmail('testonlab.us')).toEqual(false);
+ expect(service._isEmail('test@onlab')).toEqual(false);
+ });
+ });
+
describe('the _getFieldFormat method', () => {
it('should return string', () => {
expect(service._getFieldFormat('string')).toEqual('string');
});
+ it('should return mail', () => {
+ expect(service._getFieldFormat('test@onlab.us')).toEqual('email');
+ });
it('should return number', () => {
expect(service._getFieldFormat(1)).toEqual('number');
expect(service._getFieldFormat('1')).toEqual('number');
@@ -204,8 +225,13 @@
let label = angular.element(nameField.getElementsByTagName('label')[0]).text()
expect(label).toEqual('Custom Label:');
});
- });
+ it('should use the correct input type', () => {
+ expect($(element).find('[name="age"]')).toHaveAttr('type', 'number');
+ expect($(element).find('[name="birthDate"]')).toHaveAttr('type', 'date');
+ expect($(element).find('[name="enabled"]')).toHaveAttr('type', 'boolean');
+ });
+ });
});
});
})();
diff --git a/views/ngXosLib/xosHelpers/src/ui_components/dumbComponents/form.component.js b/views/ngXosLib/xosHelpers/src/ui_components/dumbComponents/form.component.js
index cd5e1ca..4f22f99 100644
--- a/views/ngXosLib/xosHelpers/src/ui_components/dumbComponents/form.component.js
+++ b/views/ngXosLib/xosHelpers/src/ui_components/dumbComponents/form.component.js
@@ -62,9 +62,9 @@
},
template: `
<ng-form name="vm.config.formName || 'form'">
- <div class="form-group" ng-repeat="field in vm.formField">
+ <div class="form-group" ng-repeat="(name, field) in vm.formField">
<label>{{field.label}}</label>
- <input type="text" name="" class="form-control" ng-model="vm.ngModel[field]"/>
+ <input type="{{field.type}}" name="{{name}}" class="form-control" ng-model="vm.ngModel[name]"/>
</div>
<div class="form-group" ng-if="vm.config.actions">
<button href=""
@@ -109,6 +109,11 @@
})
.service('XosFormHelpers', function(_, LabelFormatter){
+ this._isEmail = (text) => {
+ var re = /(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/;
+ return re.test(text);
+ };
+
this._getFieldFormat = (value) => {
// check if is date
@@ -127,6 +132,11 @@
return 'number';
}
+ // check if a string is an email
+ if(this._isEmail(value)){
+ return 'email';
+ }
+
return typeof value;
};
@@ -137,6 +147,11 @@
type: (customField[f] && customField[f].type) ? customField[f].type : this._getFieldFormat(model[f]),
validators: {}
};
+
+ if(form[f].type === 'date'){
+ model[f] = new Date(model[f]);
+ }
+
return form;
}, {});
};