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

Update ValidityState docs #26431

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open

Update ValidityState docs #26431

wants to merge 21 commits into from

Conversation

teoli2003
Copy link
Member

@teoli2003 teoli2003 commented Apr 26, 2023

Description

Adds missing pages for:

  • ValidityState.customError
  • ValidityState.valid

Motivation

Part of openwebdocs/project#153

Additional details

I also did minor fixes to the other ValidityState pages to bring them closer to the current standard. (I didn't work on extra examples there, though)

Related issues and pull requests

BCD PR (to fix mdn_url entries): mdn/browser-compat-data#19500
Fixes #26522

@github-actions github-actions bot added the Content:WebAPI Web API docs label Apr 26, 2023
@teoli2003 teoli2003 marked this pull request as ready for review April 26, 2023 10:42
@teoli2003 teoli2003 requested a review from a team as a code owner April 26, 2023 10:42
@teoli2003 teoli2003 requested review from wbamberg and Elchi3 and removed request for a team and wbamberg April 26, 2023 10:42
Copy link
Member

@Elchi3 Elchi3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Jean-Yves! Some work to do here.

files/en-us/web/api/validitystate/valid/index.md Outdated Show resolved Hide resolved
files/en-us/web/api/validitystate/valid/index.md Outdated Show resolved Hide resolved
files/en-us/web/api/validitystate/valid/index.md Outdated Show resolved Hide resolved
files/en-us/web/api/validitystate/customerror/index.md Outdated Show resolved Hide resolved
files/en-us/web/api/validitystate/customerror/index.md Outdated Show resolved Hide resolved
requestAnimationFrame(displayLog);
}

requestAnimationFrame(displayLog);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is requestAnimationFrame needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The displayLog function logs the message. It needs to be called periodically (here every frame, but we could do it less often) to update the log.

I didn't want to put it into the event handler because the event handler sets the message so, it doesn't need to read customError to know one has been set.

I've not found a simple example that would be less artificial.


### Detecting a custom error

In the following example, we set a custom error when both emails don't match. A log is written when this is the case, and a different message is displayed when there is no custom errors. When there is an error, custom or not, the widget are surrounded by a red border.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand it correctly this example doesn't really illustrate the point of this property. The point is not detection of usage but rather whether the inputs suffer from custom validity or not.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example is a bit artificial, as the function detects the error only to log a message. I've not found an idea of a simple example that would be better.

@bsmth bsmth added the awaiting response Awaiting for author to address review/feedback label Nov 13, 2023
@bsmth
Copy link
Member

bsmth commented Nov 30, 2023

Hey @teoli2003 - do you plan to come back to this one?

@teoli2003
Copy link
Member Author

Yes, but never get to the top of my notification list. I really need to block a day for "finish what is open".

teoli2003 and others added 4 commits December 20, 2023 13:33
Co-authored-by: Florian Scholz <fs@florianscholz.com>
Co-authored-by: Florian Scholz <fs@florianscholz.com>
Co-authored-by: Florian Scholz <fs@florianscholz.com>
teoli2003 and others added 4 commits December 20, 2023 13:35
Co-authored-by: Florian Scholz <fs@florianscholz.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@bsmth
Copy link
Member

bsmth commented Feb 22, 2024

@teoli2003 what's the plan with this one? Do you need a reviewer :) shall I take a look?

@github-actions github-actions bot added the size/m [PR only] 51-500 LoC changed label Mar 21, 2024
@bsmth bsmth self-requested a review April 10, 2024 11:15
@teoli2003
Copy link
Member Author

Hi! Yes I'd need a reviewer.

@bsmth bsmth removed the awaiting response Awaiting for author to address review/feedback label May 14, 2024
@bsmth bsmth self-assigned this May 14, 2024
@bsmth
Copy link
Member

bsmth commented May 14, 2024

Assigned myself so I don't forget. I'll take a look through soon 👀

@estelle
Copy link
Member

estelle commented Jun 18, 2024

@bsmth @teoli2003 bumping to the top of your notifications list ;)

@bsmth
Copy link
Member

bsmth commented Jun 21, 2024

@bsmth @teoli2003 bumping to the top of your notifications list ;)

Thanks, I've started and then put this off a few times but I'll try to look again today 😄

<input type="email" id="original" />
<label for="confirmation">Retype your email:</label>
<input type="email" id="confirmation" />
<pre id="log"></pre>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +44 to +65
const log = document.getElementById("log");
const confirmElement = document.getElementById("confirmation");

confirmElement.addEventListener("change", () => {
const originalElement = document.getElementById("original");
confirmElement.setCustomValidity(
confirmElement.value != originalElement.value
? "Both email addresses must match."
: "",
);
});

function displayLog() {
if (confirmElement.validity.customError) {
log.textContent = `There is a custom error on the repeated password: ${confirmElement.validationMessage}`;
} else {
log.textContent = "No custom error message is set.";
}
requestAnimationFrame(displayLog);
}

requestAnimationFrame(displayLog);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const log = document.getElementById("log");
const confirmElement = document.getElementById("confirmation");
confirmElement.addEventListener("change", () => {
const originalElement = document.getElementById("original");
confirmElement.setCustomValidity(
confirmElement.value != originalElement.value
? "Both email addresses must match."
: "",
);
});
function displayLog() {
if (confirmElement.validity.customError) {
log.textContent = `There is a custom error on the repeated password: ${confirmElement.validationMessage}`;
} else {
log.textContent = "No custom error message is set.";
}
requestAnimationFrame(displayLog);
}
requestAnimationFrame(displayLog);
function check(input) {
if (input.value == "good") {
input.setCustomValidity('"' + input.value + '" is not a feeling.');
} else {
// Reset the custom error message
input.setCustomValidity("");
}
}
function validateInput() {
document.getElementById("feelingInput").reportValidity();
}

Comment on lines +26 to +30
<label for="original">Type your email:</label>
<input type="email" id="original" />
<label for="confirmation">Retype your email:</label>
<input type="email" id="confirmation" />
<pre id="log"></pre>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<label for="original">Type your email:</label>
<input type="email" id="original" />
<label for="confirmation">Retype your email:</label>
<input type="email" id="confirmation" />
<pre id="log"></pre>
<input type="text" id="feelingInput" placeholder="How are you feeling?" oninput="check(this)">
<button onclick="validateInput()">Check input</button>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't think we use onEvent attributes on MDN. Do we?


### Detecting a custom error

In the following example, we set a custom error when both emails don't match. A log is written when this is the case, and a different message is displayed when there is no custom errors. When there is an error, custom or not, the widget is surrounded by a red border.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you think the suggestion below is good, we could say something like:

In the example below, setCustomValidity sets a custom error message for a field when the input is invalid. If you click the "Check input" button, we call reportValidity, which displays a validation message if the input value doesn't match the constraints. If you enter the text "good" and check the form validity, the field is marked invalid until the custom error message is cleared (set to an empty string).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bsmth
Copy link
Member

bsmth commented Jun 21, 2024

@teoli2003 some comments for you to have a look at 👀 back over to you!

@bsmth bsmth added the awaiting response Awaiting for author to address review/feedback label Jun 25, 2024

### Detecting a custom error

In the following example, we set a custom error when both emails don't match. A log is written when this is the case, and a different message is displayed when there is no custom errors. When there is an error, custom or not, the widget is surrounded by a red border.
Copy link
Member

@bsmth bsmth Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In the following example, we set a custom error when both emails don't match. A log is written when this is the case, and a different message is displayed when there is no custom errors. When there is an error, custom or not, the widget is surrounded by a red border.
In the example below, `setCustomValidity` sets a custom error message for a field when the input is invalid. If you click the "Check input" button, we call `reportValidity`, which displays a validation message if the input value doesn't match the constraints. If you enter the text "good" and check the form validity, the field is marked invalid until the custom error message is cleared (set to an empty string).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting response Awaiting for author to address review/feedback Content:WebAPI Web API docs size/m [PR only] 51-500 LoC changed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ValidityState broken link to customError
5 participants