Observing input validity - what is invalid
property used for?
#26
Comments
+1 |
2 similar comments
+1 |
+1 |
@zerodevx Out of curiosity, what is your use case for making |
@notwaldorf @zerodevx I authored the original SO post noted above. My use case was as follows: I have an element that acts as the container for a number of polymer elements used to collect and validate various user profile data (including user's email). This element is used in the app for both user registration and user profile editing, for example. Therefore, this common element is implemented by other custom/parent elements for the purposes of registration or profile editing. How each of these parent elements handle validity differs, so I need to be able to fire a custom event for the host element to uniquely act upon (i.e. implement a validity handler) whenever the validity of any of these fields change. It is helpful, for these instances, to be able to observe the validity of the entries in order to then fire the custom event for the host/parent to act upon as desired. I hope I've explained this effectively, as I'm just providing these details quickly from somewhat distant memory. |
Hi @notwaldorf - thanks for your attention; been meaning to ask... who's waldorf?? Anyhow, Observing input validityI'm quite sure there are valid use-cases for this - there's been a few +1s to this, and I think others would like to have this option as well. Say I want to pre-populate the field imperatively, and want to be notified when an error is triggered so that I can take some actions. Setting However, if the Polymer owners choose to provide this functionality, it makes more sense to instead fire a Regarding my SO answerMy original answer uses MutationObservers API to capture validity change as a workaround - it is on hindsight an anti-pattern - partly because MOs are unable to pierce through the shadow dom under native behavior. If I understand your intentions correctly, perhaps the recommended pattern is to manually perform validation - eg. <template>
...
<gold-email-input id="foo" value="{{email}}" error-message="Invalid email!!"></gold-email-input>
</template>
<script>
...
properties: {
email: { type: String, observer: "emailChanged" }
},
emailChanged: function () {
var valid = this.$.foo.validate();
if (!valid) {
// do my thing
}
}
</script> Issue when setting value imperativelyWhich brings me to the next issue - setting The problem lies between L120 - L132 - it is more generic to listen to Issue deleting email value when
|
@notwaldorf - I see you observed I will rebase, create a new issue, and re-submit a PR to fix the second. As for observing input validity, please confirm for the community's benefit that <template>
...
<gold-email-input id="foo" value="{{email}}" error-message="Invalid email!!"></gold-email-input>
</template>
<script>
...
properties: {
email: { type: String, observer: "emailChanged" }
},
emailChanged: function () {
var valid = this.$.foo.validate();
if (!valid) {
// do my thing
}
}
</script> is the recommended pattern - then this thread can be finally closed. Thanks, |
@zerodevx I think that looks fine. I don't actually have an objection to making Created: PolymerElements/paper-input#159 |
Closing this, as I think we're all sorted! |
I took interest in this SO question: http://stackoverflow.com/questions/31643650/polymer-1-0-input-validity-observer and I can certainly imagine use-cases to observe input validity.
Given that I have a template
and prototype
gold-email-input
extendsPolymer.PaperInputBehavior
, and according to this,the
invalid
property returnstrue
if the value is invalid. In this case, is there a reason whynotify: true
is not set?Similarly,
gold-email-input
usespaper-input-container
and it is not set to notify upwards as well.Lastly, in gold-email-input.html, the
invalid
attribute is set to a one-way bind from host topaper-input-container
.If I make the above 3 changes, the
invalid
property becomes observable. Is this a bug?The text was updated successfully, but these errors were encountered: