Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mwc-textfied] Setting custom validity removes the mark up for an error/helper, hence causing the form to jerk #2365

Closed
rutwick-alic opened this issue Apr 30, 2021 · 3 comments
Labels
Type: Feature New feature or request

Comments

@rutwick-alic
Copy link

I am using setCustomValidity to validate an email address onInput. When the page loads, the markup for an error/helper message is there along with the input field. Now when I use setCustomValidity to set the field as valid and set the message as blank, the markup is removed, hence causing the form to jerk.

Any ideas on how to fix this, and whether this is a bug or no?

@allan-chen allan-chen transferred this issue from material-components/material-components-web Apr 30, 2021
@taylor-vann taylor-vann self-assigned this Apr 30, 2021
@taylor-vann
Copy link
Contributor

related to 971

@taylor-vann
Copy link
Contributor

Hey @rutwick-alic thanks for submitting!

The quick solve might be to set the pattern attribute to an empty string.

Then set the validationMessage to a single space to preserve spacing.

<mwc-textfield pattern="" validationMessage=" " ></mwc-textfield>

@rutwick-alic
Copy link
Author

rutwick-alic commented May 3, 2021

Hey @rutwick-alic thanks for submitting!

The quick solve might be to set the pattern attribute to an empty string.

Then set the validationMessage to a single space to preserve spacing.

<mwc-textfield pattern="" validationMessage=" " ></mwc-textfield>

Hey @taylor-vann, thanks a lot! Let me try doing this. I will update here accordingly. Here's a quick fix that I did for the time being. I had tried setting the validationMessage to a space earlier, but it will still show as invalid, or highlight in red when you focus out.

myCustomValidator = (ev) => {
    const value = ev.target.value || null;
    if (value) {
        const re = // some regex here;
        // Get a reference to the helper mark up to add
        const helperMarkup = ev.target.shadowRoot.querySelector("#my-helper")

        if (!re.test(value)) {
            // Remove the helper div if it is already there, otherwise it will display multiple helpers
            if(helperMarkup) {
                helperMarkup.remove()
            };
            ev.target.setCustomValidity("Please enter a valid address");
            this.isFormValid = false;
        } else {
            // HACKY METHOD: Explicitly append the helper div to avoid the form from jerking due to missing html
            ev.target.setCustomValidity(""); 
            if(!helperMarkup) {
                ev.target.shadowRoot.querySelector("label").insertAdjacentHTML('afterend', '<div class="mdc-text-field-helper-line" id="my-helper"><div class="mdc-text-field-helper-text mdc-text-field-helper-text--validation-msg"><!----> <!----></div></div>')
            }
            this.isFormValid = true;
            this.value = value;
        }
    }
}

@asyncLiz asyncLiz closed this as not planned Won't fix, can't repro, duplicate, stale Sep 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants