HTML5 Forms and required fields
Hi,
I don't understand why the Form class removed the required attributes when creating the fields upon the build. (see line 618 of form.php)
HTML5 added a required attribute to the input element, but the described behavior is making impossible to set it.
I was wondering why the attribute is removed like this.
This is not the place to ask questions, that's what the forums are meant for.
It is removed for backwards compatibility because it's invalid in XHTML.
I beg to differ... This is a bug.
I'm setting the required attribute on my field and it is removed.
Explaining this with XHTML validity isn't coherent with the behavior for other attributes. Say I set the invalid-xhtml-attribute on my field, it will be shown.
Either you remove all non valid attribute or you keep them. You can't remove required because it's convenient to use this particular attributes to store information concerning the validation process.
At least put something in the documentation about that, I spent a lot of time figuring out why my attribute wasn't showing.
Sounds like a bug to me. If he is setting it, it shouldn't be removed behind is back. Making Fuel think it knows better than the developer is a recipe for disaster.
@krtek4 from a quick look through the code I think it's a leftover from something that has been removed because it was bad practice. Not sure yet if it's never used though, until that's for sure it's not as simple as just removing it because it might break Fuel BC and existing apps.
The form class does remove some things because it's field-attributes don't always map 1-on-1 to tag attributes, it's allowed for example to pass the label as an attribute. Whenever something like that is the case the keys are parsed. Which means it's not necessarily a bug, but a consequence of how Fieldset_Field objects map to tags. (an example is how passing "select" as type gives you a select tag instead of <input type='select'>)
I do however agree that it seems to be an unneccessary limitation in this case and I've re-opened this for fixing.
@bproctor don't just second or state the obvious, add something usefull, informative or don't comment. This is just clutter that doesn't add anything when someone starts fixing it.
Is it intended behaviour that this attribute is always set on fields which have a validation rule of 'required'? My understanding of this bug is that people were tying to set the required attribute on a field with set_field() and it wasn't allowing it.
This isn't a problem, I'm just checking it is what you intended to happen.
If this is intended would it be a good idea, for consistency, if other validation rules created (X)HTML params also - for example max_length?
It's a consequence of the way Validation deals with 'required', which I deemed acceptable. If you can think of a good way to add the same kind of functionality for max_length I'm all ears, but if you end up adding an elseif condition it won't get in. required has big importance for how the rule is handled in partial validations, but the actual meaning of max_length is of no interest at all to the Validation class and shouldn't get special treatment.
I'm not sure I understand why an HTML attribute has any relevance to the Validation class. Shouldn't the attributes that are output as part of the field's HTML and its validation rules be separate entities?
I see you can override this behaviour by setting the 'required' attr to null on each field manually. I don't mind the behaviour and quite like how HTML5 compatible browsers handle it but think some people will want a config value adding to disable this default behaviour.
Feel free to propose something, just be sure not to break validation.
I don't have the time right now but I'll add a feature request referencing this to remind myself.