This project is archived and is in readonly mode.
automatic client side validation with HTML 5 "required" attribute
-
ifesdjeen
+1 for that. i like the idea, and would use that, definately.
-
Rohit Arondekar
- State changed from new to open
- Importance changed from to Low
+1 Awesome idea!
But what about other validations that imply that the field is required? Will this also work for those?
-
Akira Matsuda
Updated the patch so that it applies to the current edge.
-
Jeremy Kemper
- Milestone set to 3.1
Nice!
object.class.respond_to?(:validators) && object.class.validators.any? do |v| v.is_a?(ActiveModel::Validations::PresenceValidator) && (v.attributes.include? method_name.to_sym) && v.options.keys.none? {|k| [:if, :unless].include? k} endThis is a bit of a hack though :) Perhaps this check could be extracted to Active Model.
-
Alexander S.
Just a short note, on something you are probably already aware of.
There are a few more attributes in HTML5 that are similar to required and would map nicely onto Rails model validators. Among them are min/max for input type number and range. There is also a pattern attribute which is simply a regular expression.
-
Christian Peters
I think this would be an important feature of the form helpers as it follows the DRY principle.
Though - as mentioned in the patch - it won't be possible to have full consistency between models and forms when using
:if/:unlessoption. In the model, the condition is evaluated in the context of what has happened after submitting the form and running the controller action untilobject.valid?is called. I would add a warning in thevalidates_presence_of?doc.According to Alexander's link, the required option should also be provided for text areas, check boxes and radio buttons. (The patch regards input fields only.)
-
Akira Matsuda
- Tag set to actionview, activemodel, edge, patch, radio_button_tag, rails3.1, tested, text_area_tag, text_field, validates_length_of, validates_numericality_of, validates_presence_of, validations
Thank you guys for your comments!
@Jeremy: Em... sorry for posting ugly code :< I know it was ugly, but I just wanted to know whether my idea was good or bad... I should be ashamed for that...
I just rewrote my patch taking your advice, so could you please take a look again?@Alexander S.: Thank you for you helpful comment! I implemented min/max attributes this time, but as for RegExp one, I guess it won't always work the same on server side and client side, so we need to consider a bit more deeper.
How about adding a new key for FormatValidator like:
which adds the "pattern" attribute on the HTML tag if true?:client_side => false@Christian: Yes, added the attribute for text_area and radio_button. Thanks! But check_box didn't work as expected unlike radio_button on my Opera 11 (it requires all check_boxes to be checked), so didn't touch it.
@all: Attached a new patch for required/min/max/maxlength attributes with cleaner code.
Hope you to play with it on modern browsers, and give me your feedbacks. -
Akira Matsuda
Reworded the typos on commit comments.
-
Christian Peters
@Akira: What do you mean by "it requires all check_boxes to be checked"? I was thinking about a use case like
validates_presence_of :terms_and_conditions_acceptanceresulting in the respective checkbox to have a required="required" attribute. The rest is not our business...?
