Skip to content

Commit

Permalink
Merge pull request #148 from PolymerElements/validate-on-blur
Browse files Browse the repository at this point in the history
validate inputs on blur instead of attached
  • Loading branch information
notwaldorf committed Aug 21, 2015
2 parents fae0357 + 5fb3703 commit c743170
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
2 changes: 2 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ <h4>Text area</h4>

<h4>Validation</h4>
<div class="vertical-section">
<paper-input label="input validates on blur (required, auto-validate)" required auto-validate error-message="needs some text!"></paper-input>

<paper-input label="only type letters (auto-validate)" auto-validate pattern="[a-zA-Z]*" error-message="letters only!"></paper-input>

<paper-input id="inputForValidation" required label="only type letters (no auto validate)" pattern="[a-zA-Z]*" error-message="letters only, required input!"></paper-input>
Expand Down
16 changes: 12 additions & 4 deletions paper-input-behavior.html
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,20 @@

/**
* Validates the input element and sets an error style if needed.
*
*
* @return {boolean}
*/
validate: function() {
return this.inputElement.validate();
},
validate: function() {
return this.inputElement.validate();
},

/**
* If `autoValidate` is true, then validates the element.
*/
_handleAutoValidate: function() {
if (this.autoValidate)
this.validate();
},

/**
* Restores the cursor to its original position after updating the value.
Expand Down
43 changes: 29 additions & 14 deletions paper-input-container.html
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,10 @@
return Polymer.dom(this).querySelector(this._inputSelector);
},

get _inputElementValue() {
return this._inputElement[this._propertyForValue] || this._inputElement.value;
},

ready: function() {
if (!this._addons) {
this._addons = [];
Expand All @@ -432,7 +436,12 @@
},

attached: function() {
this._handleValue(this._inputElement);
// Only validate when attached if the input already has a value.
if (this._inputElementValue != '') {
this._handleValueAndAutoValidate(this._inputElement);
} else {
this._handleValue(this._inputElement);
}
},

_onAddonAttached: function(event) {
Expand All @@ -454,28 +463,19 @@

_onBlur: function() {
this._setFocused(false);
this._handleValueAndAutoValidate(this._inputElement);
},

_onInput: function(event) {
this._handleValue(event.target);
this._handleValueAndAutoValidate(event.target);
},

_onValueChanged: function(event) {
this._handleValue(event.target);
this._handleValueAndAutoValidate(event.target);
},

_handleValue: function(inputElement) {
var value = inputElement[this._propertyForValue] || inputElement.value;

if (this.autoValidate) {
var valid;
if (inputElement.validate) {
valid = inputElement.validate(value);
} else {
valid = inputElement.checkValidity();
}
this.invalid = !valid;
}
var value = this._inputElementValue;

// type="number" hack needed because this.value is empty until it's valid
if (value || value === 0 || (inputElement.type === 'number' && !inputElement.checkValidity())) {
Expand All @@ -491,6 +491,21 @@
});
},

_handleValueAndAutoValidate: function(inputElement) {
if (this.autoValidate) {
var valid;
if (inputElement.validate) {
valid = inputElement.validate(this._inputElementValue);
} else {
valid = inputElement.checkValidity();
}
this.invalid = !valid;
}

// Call this last to notify the add-ons.
this._handleValue(inputElement);
},

_onIronInputValidate: function(event) {
this.invalid = this._inputElement.invalid;
},
Expand Down

0 comments on commit c743170

Please sign in to comment.