
Proposed fix for CheckboxField
Reported by Locv39KnvwDeXGpKpisHDaKw | April 11th, 2009 @ 09:31 PM
I was having an issue with Webrat's handling of checkboxes. For some reason Rails was seeing a single checkbox as checked when it wasn't. Specifically, Rails was accepting a form submission, but it should've been redirecting back to the form. (The checkbox is for a terms of service agreement.)
After making some minor changes in field.rb it seems like it's working as expected. Any thoughts on the code below?
webrat/core/elements/field.rb:
class CheckboxField < Field #:nodoc:
attr_reader :checked
def initialize(*args)
super
pre_checked = Webrat::XML.attribute(@element, "checked") == "checked"
@checked = pre_checked ? "checked" : ""
if pre_checked
set(Webrat::XML.attribute(@element, "value") || "on")
else
set("")
end
end
def self.xpath_search
".//input[@type = 'checkbox']"
end
def to_param
return nil if @value.nil?
super
end
def check
raise_error_if_disabled
@checked = "checked"
set(Webrat::XML.attribute(@element, "value") || "on")
end
def uncheck
raise_error_if_disabled
@checked = ""
set("")
end
def checked?
@checked == "checked"
end
protected
def default_value
if Webrat::XML.attribute(@element, "checked") == "checked"
Webrat::XML.attribute(@element, "value") || "on"
else
nil
end
end
end
Comments and changes to this ticket
-
gaffo May 7th, 2009 @ 06:42 PM
- Tag changed from check, checkbox, field, patch to check, checkbox, field, patch, verify
- State changed from new to open
Could you please resubmit this as a patch or a pull request. We can't tell what version of the file you pulled this from and so can't tell what you changed.
Thanks
-
gaffo June 15th, 2009 @ 12:20 AM
- Tag changed from check, checkbox, field, patch, verify to check, checkbox, field, patch, stale, verify
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.