Skip to content

Commit

Permalink
Merge pull request #435 from marmelab/choice_field_required
Browse files Browse the repository at this point in the history
[RFR] Make choice field required works and fix angular add empty first option
  • Loading branch information
fzaninotto committed May 15, 2015
2 parents 08ea55d + 9201c70 commit e53e6ac
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 63 deletions.
3 changes: 2 additions & 1 deletion examples/blog/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@
.label('Post')
.map(truncate)
.targetEntity(post)
.targetField(nga.field('title')),
.targetField(nga.field('title'))
.validation({ required: true }),
]);

comment.editionView()
Expand Down
8 changes: 3 additions & 5 deletions src/javascripts/ng-admin/Crud/field/maChoiceField.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ define(function (require) {
}
},
template:
'<select ng-model="value" ng-required="v.required" id="{{ name }}" name="{{ name }}" class="form-control">' +
'<option ng-if="!v.required" value="" ng-selected="!value">-- select a value --</option>' +
'<option ng-repeat="choice in getChoices(entry)" value="{{ choice.value }}" ng-selected="value == choice.value">' +
'{{ choice.label }}' +
'</option>' +
'<select ng-model="value" ng-required="v.required" id="{{ name }}" name="{{ name }}" class="form-control"' +
' ng-options="item.value as item.label for item in getChoices(entry)">' +
'<option value="">-- select a value --</option>' +
'</select>'
};
}
Expand Down
19 changes: 2 additions & 17 deletions src/javascripts/ng-admin/Crud/field/maChoicesField.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,14 @@ define(function (require) {
for (var name in attributes) {
select[name] = attributes[name];
}
scope.contains = contains;
},
template:
'<select multiple ng-model="value" id="{{ name }}" name="{{ name }}" class="form-control" ng-required="v.required">' +
'<option ng-repeat="choice in getChoices(entry)" value="{{ choice.value }}" ng-selected="contains(value, choice.value)">' +
'{{ choice.label }}' +
'</option>' +
'<select multiple ng-model="value" id="{{ name }}" name="{{ name }}" class="form-control" ng-required="v.required"' +
' ng-options="item.value as item.label for item in getChoices(entry)">' +
'</select>'
};
}

function contains (collection, item) {
if (!collection) {
return false;
}
for (var i = 0, l = collection.length; i < l; i++) {
if (collection[i] == item) { // == is intentional here
return true;
}
}
return false;
}

maChoicesField.$inject = [];

return maChoicesField;
Expand Down
8 changes: 4 additions & 4 deletions src/javascripts/test/e2e/filterViewSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ describe('Global filter', function () {
});

it('should filter on reference', function () {
// Filter on post_id '3'
$$('.filters .filter select option[value="3"]').click();
// Filter on post_id '3' (Perspiciatis adipisci vero qui ipsam iure porro)
$$('.filters .filter select option[value="9"]').click();
$$('.filters button[type="submit"]').click();
$$('.grid tr td:nth-child(4)').then(function (tdElements) {
expect(tdElements.length).toBe(2);
Expand All @@ -63,8 +63,8 @@ describe('Global filter', function () {
});

it('should update the pagination total', function () {
// Filter on post id '3'
$$('.filters .filter select option[value="3"]').click();
// Filter on post id '3' (Perspiciatis adipisci vero qui ipsam iure porro)
$$('.filters .filter select option[value="9"]').click();
$$('.filters button[type="submit"]').click();
$$('ma-datagrid-pagination .total').then(function (totalElements) {
expect(totalElements[0].getText()).toBe('1 - 2 on 2');
Expand Down
31 changes: 13 additions & 18 deletions src/javascripts/test/unit/Crud/field/maChoiceFieldSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,8 @@ define(function (require) {
var element = $compile(directiveUsage)(scope);
scope.$digest();
var options = element.find('option');
// strange: angular.js adds an initial option of value '? value ?' for IE compatibility
// so our initial option is of index 1, not 0
expect(options[1].innerHTML).toEqual('-- select a value --');
expect(options[1].value).toEqual('');
expect(options[1].selected).toBeTruthy();
expect(options[0].innerHTML).toEqual('-- select a value --');
expect(options[0].value).toEqual('');
});

it("should provide an initial option for non-required fields", function () {
Expand All @@ -56,10 +53,8 @@ define(function (require) {
var element = $compile(directiveUsage)(scope);
scope.$digest();
var options = element.find('option');
// strange: angular.js adds an initial option of value '? value ?' for IE compatibility
// so our initial option is of index 1, not 0
expect(options[1].innerHTML).toEqual('foo');
expect(options[1].value).toEqual('bar');
expect(options[1].label).toEqual('foo');
expect(options[1].value).toEqual('0');
});

it("should contain the choices as options", function () {
Expand All @@ -71,10 +66,10 @@ define(function (require) {
var element = $compile(directiveUsage)(scope);
scope.$digest();
var options = element.find('option');
expect(options[1].innerHTML).toEqual('foo');
expect(options[1].value).toEqual('bar');
expect(options[2].innerHTML).toEqual('baz');
expect(options[2].value).toEqual('bazValue');
expect(options[1].label).toEqual('foo');
expect(options[1].value).toEqual('0');
expect(options[2].label).toEqual('baz');
expect(options[2].value).toEqual('1');
});

it("should contain the choices from choicesFunc as options", function () {
Expand All @@ -89,10 +84,10 @@ define(function (require) {
var element = $compile(directiveUsage)(scope);
scope.$digest();
var options = element.find('option');
expect(options[1].innerHTML).toEqual('foo');
expect(options[1].value).toEqual('bar');
expect(options[2].innerHTML).toEqual('baz');
expect(options[2].value).toEqual('bazValue');
expect(options[1].label).toEqual('foo');
expect(options[1].value).toEqual('0');
expect(options[2].label).toEqual('baz');
expect(options[2].value).toEqual('1');
});

it("should pass entry to choicesFunc", function () {
Expand All @@ -119,7 +114,7 @@ define(function (require) {
scope.$digest();
var options = element.find('option');
expect(options[2].selected).toBeTruthy();
expect(options[2].value).toEqual('bazValue');
expect(options[2].value).toEqual('1');
});

});
Expand Down
22 changes: 11 additions & 11 deletions src/javascripts/test/unit/Crud/field/maChoicesFieldSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ define(function (require) {
var element = $compile(directiveUsage)(scope);
scope.$digest();
var options = element.find('option');
expect(options[0].innerHTML).toEqual('foo');
expect(options[0].value).toEqual('bar');
expect(options[1].innerHTML).toEqual('baz');
expect(options[1].value).toEqual('bazValue');
expect(options[0].label).toEqual('foo');
expect(options[0].value).toEqual('0');
expect(options[1].label).toEqual('baz');
expect(options[1].value).toEqual('1');
});

it("should contain the choices as options", function () {
Expand All @@ -79,10 +79,10 @@ define(function (require) {
var element = $compile(directiveUsage)(scope);
scope.$digest();
var options = element.find('option');
expect(options[0].innerHTML).toEqual('foo');
expect(options[0].value).toEqual('bar');
expect(options[1].innerHTML).toEqual('baz');
expect(options[1].value).toEqual('bazValue');
expect(options[0].label).toEqual('foo');
expect(options[0].value).toEqual('0');
expect(options[1].label).toEqual('baz');
expect(options[1].value).toEqual('1');
});

it("should have the options with the bounded value selected", function () {
Expand All @@ -95,11 +95,11 @@ define(function (require) {
var element = $compile(directiveUsage)(scope);
scope.$digest();
var options = element.find('option');
expect(options[0].value).toEqual('fooValue');
expect(options[0].value).toEqual('0');
expect(options[0].selected).toBeTruthy();
expect(options[1].value).toEqual('barValue');
expect(options[1].value).toEqual('1');
expect(options[1].selected).toBeFalsy();
expect(options[2].value).toEqual('bazValue');
expect(options[2].value).toEqual('2');
expect(options[2].selected).toBeTruthy();
});

Expand Down
7 changes: 0 additions & 7 deletions src/sass/ng-admin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,6 @@ div.bottom-loader {
}
}

/**
* Fields
*/
ma-choice-field select option:empty {
display:none;
}

/**
* Edition form
*/
Expand Down

0 comments on commit e53e6ac

Please sign in to comment.