Permalink
Browse files

Merge branch 'attr-init-val' into dev-master

Conflicts:
	src/attribute/HISTORY.md
  • Loading branch information...
2 parents 764a5bf + 9ac46fb commit 0d04a68ec5808c1454dbf78ca34dc793b4964f20 @ericf ericf committed Jan 8, 2013
View
4 src/attribute/HISTORY.md
@@ -4,6 +4,10 @@ Attribute Change History
@VERSION@
-----
+* Invalid values supplied during Attribute initialization that fail setter
+ validation will now fallback the default value defined in `ATTRS`.
+ [Ticket #2528732] [redbat]
+
* Attribute validators and setters now receive set's `options` argument. This is
now a part of `AttributeCore`. [Ticket #2532810] [Satyam]
View
9 src/attribute/js/AttributeCore.js
@@ -630,8 +630,13 @@
retVal = setter.call(host, newVal, name, opts);
if (retVal === INVALID_VALUE) {
- Y.log('Attribute: ' + attrName + ', setter returned Attribute.INVALID_VALUE for value:' + newVal, 'warn', 'attribute');
- allowSet = false;
+ if (initializing) {
+ Y.log('Attribute: ' + attrName + ', setter returned Attribute.INVALID_VALUE for value:' + newVal + ', initializing to default value', 'warn', 'attribute');
+ newVal = cfg.defaultValue;
+ } else {
+ Y.log('Attribute: ' + attrName + ', setter returned Attribute.INVALID_VALUE for value:' + newVal, 'warn', 'attribute');
+ allowSet = false;
+ }
} else if (retVal !== undefined){
Y.log('Attribute: ' + attrName + ', raw value: ' + newVal + ' modified by setter to:' + retVal, 'info', 'attribute');
newVal = retVal;
View
75 src/attribute/tests/unit/assets/attribute-core-tests.js
@@ -95,7 +95,7 @@ YUI.add('attribute-core-tests', function(Y) {
function FooBar(userVals) {
Y.Attribute.call(this, null, userVals);
- };
+ }
FooBar.ATTRS = {
foo:{
@@ -404,6 +404,79 @@ YUI.add('attribute-core-tests', function(Y) {
Y.Assert.areEqual(undefined, h.get("complex.Y.A"));
},
+ testDefaultSet: function() {
+ function FooBar(userVals) {
+ Y.Attribute.call(this, null, userVals);
+ }
+
+ FooBar.ATTRS = {
+ foo: {
+ value: 'foo',
+
+ setter: function (v) {
+ if (v !== 'A' && v !== 'B') {
+ return Y.Attribute.INVALID_VALUE;
+ }
+
+ return v;
+ }
+ },
+
+ bar: {
+ value: 'bar',
+
+ validator: function (v) {
+ return (v === 'A' || v === 'B');
+ }
+ }
+ };
+
+ // Straightup augment, no wrapper functions
+ Y.mix(FooBar, Y.Attribute, false, null, 1);
+
+ var h = new FooBar({
+ foo: 'zee',
+ bar: 'zee'
+ });
+
+ Y.Assert.areNotSame(undefined, h.get('foo'));
+ Y.Assert.areSame('foo', h.get('foo'));
+ Y.Assert.areNotSame(undefined, h.get('bar'));
+ Y.Assert.areSame('bar', h.get('bar'));
+
+ h.set('foo', 'invalid again');
+ h.set('bar', 'invalid again');
+ Y.Assert.areSame('foo', h.get('foo'));
+ Y.Assert.areSame('bar', h.get('bar'));
+
+ h.set('foo', 'A');
+ h.set('bar', 'A');
+ Y.Assert.areSame('A', h.get('foo'));
+ Y.Assert.areSame('A', h.get('bar'));
+
+ h = new FooBar({
+ foo: 'B',
+ bar: 'B'
+ });
+
+ Y.Assert.areNotSame(undefined, h.get('foo'));
+ Y.Assert.areNotSame('foo', h.get('foo'));
+ Y.Assert.areSame('B', h.get('foo'));
+ Y.Assert.areNotSame(undefined, h.get('bar'));
+ Y.Assert.areNotSame('bar', h.get('bar'));
+ Y.Assert.areSame('B', h.get('bar'));
+
+ h.set('foo', 'invalid');
+ h.set('bar', 'invalid');
+ Y.Assert.areSame('B', h.get('foo'));
+ Y.Assert.areSame('B', h.get('bar'));
+
+ h.set('foo', 'A');
+ h.set('bar', 'A');
+ Y.Assert.areSame('A', h.get('foo'));
+ Y.Assert.areSame('A', h.get('bar'));
+ },
+
testInitialValidation: function() {
var h = this.createHost({A:5});
Y.Assert.areEqual("AVal", h.get("A")); // Numerical value validation failure should revert to default value

0 comments on commit 0d04a68

Please sign in to comment.