Skip to content

Commit b34c14a

Browse files
committed
Merge pull request #26 from PolymerElements/on-demand-validation
allow manual validation
2 parents 23dc7b0 + c4138f9 commit b34c14a

File tree

4 files changed

+66
-9
lines changed

4 files changed

+66
-9
lines changed

demo/index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@
6464

6565
</section>
6666

67+
<section>
68+
69+
<div>Manual Validation</div>
70+
71+
<paper-input id="inputForValidation" required label="only type letters" pattern="[a-zA-Z]*" error-message="letters only, required input!"></paper-input>
72+
<br>
73+
<button onclick="validate()">Validate!</button>
74+
75+
</section>
76+
6777
<section>
6878

6979
<div>Character Counter</div>
@@ -94,4 +104,10 @@
94104

95105
</section>
96106

107+
<script>
108+
function validate() {
109+
document.getElementById('inputForValidation').validate();
110+
}
111+
</script>
97112
</body>
113+
</html>

paper-input-behavior.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@
180180
return this.$.input;
181181
},
182182

183+
/**
184+
* Validates the input element and sets an error style if needed.
185+
*/
186+
validate:function () {
187+
return this.inputElement.validate();
188+
},
189+
183190
_computeAlwaysFloatLabel: function(alwaysFloatLabel, placeholder) {
184191
return placeholder || alwaysFloatLabel;
185192
}

paper-input-container.html

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@
311311
},
312312

313313
listeners: {
314-
'addon-attached': '_onAddonAttached'
314+
'addon-attached': '_onAddonAttached',
315+
'iron-input-validate': '_onInputValidityChanged'
315316
},
316317

317318
get _valueChangedEvent() {
@@ -365,10 +366,13 @@
365366
var value = inputElement[this._propertyForValue] || inputElement.value;
366367

367368
var valid;
368-
if (inputElement.validate) {
369-
valid = inputElement.validate(value);
370-
} else if (inputElement.checkValidity) {
371-
valid = inputElement.checkValidity();
369+
370+
if (this.autoValidate) {
371+
if (inputElement.validate) {
372+
valid = inputElement.validate(value);
373+
} else {
374+
valid = inputElement.checkValidity();
375+
}
372376
} else {
373377
valid = true;
374378
}
@@ -380,10 +384,6 @@
380384
this._inputHasContent = false;
381385
}
382386

383-
if (this.autoValidate) {
384-
this.invalid = !valid;
385-
}
386-
387387
// notify add-ons
388388
for (var addon, i = 0; addon = this._addons[i]; i++) {
389389
// need to set all of these, or call method... thanks input type="number"!
@@ -393,6 +393,15 @@
393393
}
394394
},
395395

396+
_onInputValidityChanged: function() {
397+
this.invalid = this._inputElement.invalid;
398+
399+
// notify add-ons
400+
for (var addon, i = 0; addon = this._addons[i]; i++) {
401+
addon.invalid = this.invalid;
402+
}
403+
},
404+
396405
_computeInputContentClass: function(noLabelFloat, alwaysFloatLabel, focused, invalid, _inputHasContent) {
397406
var cls = 'input-content';
398407
if (!noLabelFloat) {

test/paper-input-container.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@
7474
</template>
7575
</test-fixture>
7676

77+
<test-fixture id="manual-validate-numbers">
78+
<template>
79+
<paper-input-container attr-for-value="bind-value">
80+
<label id="l">label</label>
81+
<input is="iron-input" id="i" pattern="[0-9]*">
82+
</paper-input-container>
83+
</template>
84+
</test-fixture>
85+
7786
<letters-only></letters-only>
7887

7988
<test-fixture id="auto-validate-validator">
@@ -200,6 +209,22 @@
200209
assert.isTrue(Polymer.dom(container.root).querySelector('.underline').classList.contains('is-invalid'), 'underline has is-invalid class');
201210
});
202211

212+
test('styled when the input is set to an invalid value with manual validation', function(done) {
213+
var container = fixture('manual-validate-numbers');
214+
var input = Polymer.dom(container).querySelector('#i');
215+
var label = Polymer.dom(container).querySelector('#l');
216+
var line = Polymer.dom(container.root).querySelector('.focused-line');
217+
var color = getComputedStyle(label).color;
218+
line.addEventListener('transitionend', function() {
219+
assert.isTrue(container.invalid, 'invalid is true');
220+
assert.notEqual(getComputedStyle(label).color, color, 'label is colored when input is invalid');
221+
assert.equal(getTransform(line), 'none', 'line is colored when input is invalid');
222+
done();
223+
});
224+
input.bindValue = 'foobar';
225+
input.validate();
226+
});
227+
203228
});
204229

205230
</script>

0 commit comments

Comments
 (0)