[whatwg] Form element invalid message

On 09/22/2010 02:51 PM, Aryeh Gregor wrote:
> data:text/html,<!doctype html><form><input name=x required
> oninvalid="this.setCustomValidity(''); if (!this.validity.valid)
> this.setCustomValidity('abcd')"> <input type=submit></form>
> 
> In a Firefox 4 nightly, when I click the submit button, the error is
> just the string "abcd".  In Opera it's worse -- "The value  is not
> allowed by a script on the page! abcd" (visible double space after
> "value", due to inserting the string unquoted) -- but the Opera UI
> here is really bad in many ways, as noted, and is likely to improve as
> other browsers implement good UIs.  Note that Firefox is buggy here
> and treats setCustomValidity('') as setting the error message to ''
> instead of removing it, as the spec says, but when that's fixed it
> will work.

I don't think there is a bug in Gecko. For what I understand, the bug is
in Presto if the behavior is what you describe.
According to the specifications [1], when the submission is requested
and there are invalid form elements, the form submission should be
canceled. If one element do not cancel the invalid event, the UA should
use it's own interface to explain what's happening. In all cases, the
form submission will be canceled.

So, what you do is making the element valid in the invalid event which
is too late. After the invalid event, Firefox tries to show the UI using
the validationMessage which return the empty string when the form is
valid. You should cancel the event if you want to have no UI at all but
still cancel the submission. You should use onchange/oninput to change
the validity state if you want the form to be submitted.

And, FTR, I think I do not think it's a good think to use
setCustomValidity() _only_ to put your own message in there. This should
be used when the validity rule isn't one of those specified. For
example, if you want two password fields to be the same, you can use
setCustomValidity() because there is no way to specify that with the
current constraint validation API.
I don't think people should promote the use of setCustomValidity() to
override a pre-defined message with a more custom one like changing
"Please fill this field." by "Please, set a username.". But that's just
an opinion...

[1]
http://www.whatwg.org/specs/web-apps/current-work/multipage/association-of-controls-and-forms.html#interactively-validate-the-constraints

Thanks,
--
Mounir

Received on Wednesday, 22 September 2010 17:50:20 UTC