Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added CDATA tags around form helper Javascript #295

Closed
wants to merge 7 commits into from

3 participants

@stuartc

When running view tests, I was getting piles of warnings like this from assert_select:

ignoring attempt to close div with script
 opened at byte 15673, line 411
 closed at byte 18702, line 411
 attributes at open: {"class"=>"\\\"field_with_errors\\\""}
 text around open: "ilder\",\"input_tag\":\"<div class=\\\"field_w"
 text around close: "act.processing\"}}}};</script>\n</fieldset"

It seems that most parsers/assert_select can't discern embedded HTML inside a script tag.
After some fiddling I added some CDATA tags on the inside of the script tag the form_helper adds to
the bottom of the form. All my JS still worked, and all my capybara tests still passed.

It's a pretty minor change.

@stuartc

Apologies, going to resubmit this, asked for a pull off master.

@stuartc stuartc closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
8 coffeescript/rails.validations.coffee
@@ -24,7 +24,7 @@ $.fn.validate = ->
'form:validate:fail' : (eventData) -> ClientSideValidations.callbacks.form.fail( form, eventData)
'form:validate:pass' : (eventData) -> ClientSideValidations.callbacks.form.pass( form, eventData)
}
- form.find('[data-validate="true"]:input:enabled:not(:radio)').live(event, binding) for event, binding of {
+ form.find('[data-validate="true"]:input:not(:radio)').live(event, binding) for event, binding of {
'focusout': -> $(@).isValid(settings.validators)
'change': -> $(@).data('changed', true)
# Callbacks
@@ -66,8 +66,8 @@ validateForm = (form, validators) ->
form.trigger('form:validate:before')
valid = true
- form.find('[data-validate="true"]:input:enabled').each ->
- valid = false if $(@).isValid(validators)
+ form.find('[data-validate="true"]:input').each ->
+ valid = false unless $(@).isValid(validators)
if valid then form.trigger('form:validate:pass') else form.trigger('form:validate:fail')
@@ -102,7 +102,7 @@ validateElement = (element, validators) ->
element.trigger('element:validate:pass')
element.trigger('element:validate:after')
- element.data('valid') == false ? false : true
+ element.data('valid') != false
# Main hook
# If new forms are dynamically introduced into the DOM the .validate() method
View
3  lib/client_side_validations/action_view/form_helper.rb
@@ -95,9 +95,8 @@ def client_side_form_settings(object, options)
end
content_tag(:script) do
- "window.ClientSideValidations.forms['#{var_name}'] = #{builder.client_side_form_settings(options, self).merge(:validators => 'validator_hash').to_json};".html_safe
+ "//<![CDATA[\nwindow.ClientSideValidations.forms['#{var_name}'] = #{builder.client_side_form_settings(options, self).merge(:validators => 'validator_hash').to_json};\n//]]>".html_safe
end
-
end
end
View
2  test/action_view/cases/helper.rb
@@ -145,7 +145,7 @@ def whole_form(action = "http://www.example.com", id = nil, html_class = nil, op
end
def build_script_tag(html, id, validators)
- (html || "") + %Q{<script>window.ClientSideValidations.forms['#{id}'] = #{client_side_form_settings_helper.merge(:validators => validators).to_json};</script>}
+ (html || "") + %Q{<script>//<![CDATA[\nwindow.ClientSideValidations.forms['#{id}'] = #{client_side_form_settings_helper.merge(:validators => validators).to_json};\n//]]></script>}
end
protected
View
9 test/javascript/public/test/validateElement.js
@@ -294,3 +294,12 @@ test("Don't validate dynamically disabled inputs", function() {
input.trigger('focusout');
ok(!input.parent().hasClass('field_with_errors'));
});
+
+test("Return validation result", function() {
+ var input = $('#user_name');
+
+ ok(!input.isValid(ClientSideValidations.forms['new_user'].validators));
+
+ input.val('123').data('changed', true);
+ ok(input.isValid(ClientSideValidations.forms['new_user'].validators));
+});
View
12 vendor/assets/javascripts/rails.validations.js
@@ -69,7 +69,7 @@
};
for (event in _ref2) {
binding = _ref2[event];
- form.find('[data-validate="true"]:input:enabled:not(:radio)').live(event, binding);
+ form.find('[data-validate="true"]:input:not(:radio)').live(event, binding);
}
form.find('[data-validate="true"]:checkbox').live('click', function() {
return $(this).isValid(settings.validators);
@@ -112,8 +112,8 @@
var valid;
form.trigger('form:validate:before');
valid = true;
- form.find('[data-validate="true"]:input:enabled').each(function() {
- if ($(this).isValid(validators)) return valid = false;
+ form.find('[data-validate="true"]:input').each(function() {
+ if (!$(this).isValid(validators)) return valid = false;
});
if (valid) {
form.trigger('form:validate:pass');
@@ -125,7 +125,7 @@
};
validateElement = function(element, validators) {
- var context, fn, kind, message, valid, _ref;
+ var context, fn, kind, message, valid;
element.trigger('element:validate:before');
if (element.data('changed') !== false) {
valid = true;
@@ -156,9 +156,7 @@
}
}
element.trigger('element:validate:after');
- return (_ref = element.data('valid') === false) != null ? _ref : {
- "false": true
- };
+ return element.data('valid') !== false;
};
$(function() {
Something went wrong with that request. Please try again.