While reading the specs for #1445224: Add new HTML5 FAPI element: color I found that some attributes are not allowed for the new input types (making for invalid HTML).

Reference for the specific states: http://www.w3.org/TR/html5/number-state.html

Comments

Niklas Fiekas’s picture

Status: Active » Needs review
StatusFileSize
new10.86 KB

First try.

bleen’s picture

Status: Needs review » Needs work
Issue tags: +Needs documentation
+++ b/core/includes/form.incundefined
@@ -4032,6 +4032,18 @@ function form_validate_number(&$element, &$form_state) {
+function form_validate_range(&$element, &$form_state) {
+  if ($element['#value'] === '') {
+    form_set_value($element, 0.0, $form_state);
+  }

This confuses me a bit... if there is no way to submit a form with a range field left blank, then why is this function needed. If there is a way, then shouldn't we still keep #required?

Assuming there is a good reason for this function, what if 0 is not in between #min & #max?

-17 days to next Drupal core point release.

attiks’s picture

@bleen18, it needs to be check on the server side as well, nothing stop me from changing the html so I can bypass it.

I agree with your second point and I think it would be better to throw an error when there's no value, don't assume it's 0.

Niklas Fiekas’s picture

Status: Needs work » Needs review
StatusFileSize
new1.01 KB
new11.16 KB

Good catch, thank you.

Changed the doxygen to say the 'required' attribute is not allowed. It's not really that #required is not supported by Drupal, but that adding the 'required' attribute would make invalid HTML.

The doc says the default value should be minimum plus half the difference between the minimum and the maximum. That is the same thing as (#min + #max) / 2. We also must round the value to match #step.

Niklas Fiekas’s picture

+++ b/core/includes/form.incundefined
@@ -4032,6 +4032,26 @@ function form_validate_number(&$element, &$form_state) {
+ * Make sure range elements always have a value. Default to 0. The 'required'

Accidantely left "Default to 0." in.

attiks’s picture

I read the specs, is the following handled: The default value is the minimum plus half the difference between the minimum and the maximum, unless the maximum is less than the minimum, in which case the default value is the minimum.

bleen’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

As you already stated in #5, the doc block needs to be fixed ... (setting to needs work for this)

Other than that, what is the expected behavior for a range field left empty with a min of 2, max of 8 and a step of 2?

Also, I think this need a test - though Im not sure how since the only way to submit an empty rang field is by manipulating the html

Niklas Fiekas’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new3 KB
new14.19 KB
new4.15 KB

Alright, here are tests. (And they helped me catch at least one more mistake, so far.)

Other than that, what is the expected behavior for a range field left empty with a min of 2, max of 8 and a step of 2?

I added excatly that scenario to the tests as well. Let's see: The mean of #min and #max is 5. That is 1.5 step from 2. Round the number of steps to 2. 2+ 2 * 2 = 6. The intresting part is of course the decision between 1 step or 2 steps, so I double checked webkit does the same thing.

unless the maximum is less than the minimum, in which case the default value is the minimum.

Good point, attiks. I'd say we should ignore this state, since it won't pass #min and #max validation anyway. Browsers skip that validation then, but I believe we shouldn't ignore #min and #max in that case. Otherwise it might be hard to spot such a mistake.

Status: Needs review » Needs work

The last submitted patch, 1540174-html5-attributes-8-tests.patch, failed testing.

Niklas Fiekas’s picture

Status: Needs work » Needs review
bleen’s picture

Good point, attiks. I'd say we should ignore this state, since it won't pass #min and #max validation anyway. Browsers skip that validation then, but I believe we shouldn't ignore #min and #max in that case. Otherwise it might be hard to spot such a mistake.

Can you please explain this ... I'm lost

Niklas Fiekas’s picture

Alright. So what happens with:
<input type="range" min="10" max="5" />
Apparently no value can meet both min and max. That's why min > max is invalid. No client side validation will happen in that case, as if min and max weren't provided at all.

Now: What happens on the server side?
I think we should not skip validation, instead simply fail for all values. Silently skipping validation might lead to problems that are hard to find.

If we do agree on that, it doesn't matter what default value we choose when min > max, since no value can pass validation anyway.

bleen’s picture

Status: Needs review » Reviewed & tested by the community

Got it ... then with that, I'm sold!!

  • code looks good
  • tests look good
  • manual testing works

RTBC

attiks’s picture

Status: Reviewed & tested by the community » Needs work

#12

No client side validation will happen in that case, as if min and max weren't provided at all.

Both chrome and Opera try to validate, but tho only thing that can pass is an empty field.

The specs says that the default value in this case has to be the min, so this has to be added. Now it's set to 5 (see code below).

  $form['maxmin'] = array(
    '#type' => 'range',
    '#title' => 'min greater than max',
    '#min' => 10,
    '#max' => 1,
  );
attiks’s picture

Status: Needs work » Needs review
StatusFileSize
new14.4 KB

Patch with new test added, validation isn't changed so it will fail

Status: Needs review » Needs work

The last submitted patch, i1540174.patch, failed testing.

Niklas Fiekas’s picture

Status: Needs work » Needs review

No, I believe this is wrong.

Both chrome and Opera try to validate

Actually try this:

<form>
  <input type="range" min="10" max="1" name="foo" />
  <input type="submit" />
</form>

Chrome will show a slider from 0 to 100 and will let you submit any integer between those limits, which is the behaviour as if min or max weren't given.

but tho only thing that can pass is an empty field.

Range elements can't be empty, which makes sense, because in what state would a slider be, to be empty?
That means no value at all could pass validation (if it happened). And because it should happen at least on the server side, no value can pass. Not even the default value. So there is no point in calculating that value.

Thank you for the added test, though. It shows a different problem:

  • Send the form with an empty value.
  • The default value gets calculated. Even 10, if you wish.
  • The validation should now fail, but it is skipped, because $element['#value'] is still '' in form_number_validate().
Niklas Fiekas’s picture

attiks’s picture

Status: Needs review » Needs work

You're right, i was thinking about number. But nevertheless if my browser doesn't understand html5 it will show me a textfield without any validation, so we need to check it on the server side?

Niklas Fiekas’s picture

Exactly, for that and against malicious forgery :)

attiks’s picture

and shouldn't we follow the specs and set the default value, even worse in the case min > max the only allowed value is min?

ericduran’s picture

Status: Needs work » Reviewed & tested by the community

Patch in number #8 looks good to me too.

attiks’s picture

Status: Reviewed & tested by the community » Needs review

@ericduran the patch doesn't set the default value right when min > max. i know it's edge case but it can be added.

ericduran’s picture

I'll leave it to other people to decided. But the only way you'll get a min value that's greater than a max value is if a developer sets the wrong value.

As far as I'm aware core has never tried to protect itself from this. In other words we would not write code to protect from a developer using wrong values and/or putting a min number that's greater than the max.

Does this make sense? At least this is my understanding. I'll leave it to someone else to decided but I say we don't worry about this edge case.

attiks’s picture

Status: Needs review » Reviewed & tested by the community

I created a new issue #1542258: Decide what to do when min > max for HTML5 number/range element to discuss this, so this is RTBC

Niklas Fiekas’s picture

Alright ... let's get this issue cleaned up. Since we're not in a hurry, I go back from RTBC.

I should have highlighted #17:
[...] Thank you for the added test, though. It shows a different problem: [...]

I added a test (based on attiks test - and thank you again) that shows that we also have to update $element['#value'] for the following validation.

1540174-html5-attributes-26.patch should fix everything.

With that fix invalid ranges won't get past validations. That probably means we can also close #1542258: Decide what to do when min > max for HTML5 number/range element. Like: "What value will get passed?" "I don't know, it doesn't even get to the submit handler." But we can discuss that there.

Edit: Yay! Test results as expected. Exactly one fail for #17, 4 fails for the patch without tests and the combined patch is green.

bleen’s picture

Status: Needs review » Reviewed & tested by the community

back to RTBC

xjm’s picture

See also: #1542800-13: Add missing @param in includes D-G (regarding the validation handlers).

no_commit_credit’s picture

StatusFileSize
new528 bytes
new15.1 KB

Trivial cleanup.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, html5-1540174-30.patch, failed testing.

xjm’s picture

Status: Needs work » Reviewed & tested by the community

Random failure is from #1361234: Make the node entity a classed object. I requeued the test.

chx’s picture

Status: Reviewed & tested by the community » Needs work

form_validate_range why is this not a per type value callback? It does not seem to validate.

Niklas Fiekas’s picture

Status: Needs work » Needs review
StatusFileSize
new2.19 KB
new15.15 KB
new3.85 KB

Awesome, perfect fit, thanks!

Didn't even know such a thing existed.

Niklas Fiekas’s picture

Fix typo.

attiks’s picture

Status: Needs review » Needs work
+++ b/core/includes/form.incundefined
@@ -4032,6 +4032,36 @@ function form_validate_number(&$element, &$form_state) {
+ * @param $form

param is $element not $form in the code.

Niklas Fiekas’s picture

Status: Needs work » Needs review
StatusFileSize
new876 bytes
new15.15 KB

Another good catch. Thanks. form_type_checkbox_value() that I used as a template seems to confuse $form and $element, too. (There's a clean-up issue already.)

no_commit_credit’s picture

StatusFileSize
new524 bytes
new15.15 KB

Just touching up a verb form: http://drupal.org/node/1354#functions

attiks’s picture

Status: Needs review » Reviewed & tested by the community

This looks good to me.

catch’s picture

Status: Reviewed & tested by the community » Fixed

Looks good. Committed/pushed to 8.x.

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

Add reference link