aboutsummaryrefslogtreecommitdiffstats
path: root/actionview/lib/action_view/helpers/tags/color_field.rb
diff options
context:
space:
mode:
Diffstat (limited to 'actionview/lib/action_view/helpers/tags/color_field.rb')
-rw-r--r--actionview/lib/action_view/helpers/tags/color_field.rb27
1 files changed, 27 insertions, 0 deletions
diff --git a/actionview/lib/action_view/helpers/tags/color_field.rb b/actionview/lib/action_view/helpers/tags/color_field.rb
new file mode 100644
index 0000000000..39ab1285c3
--- /dev/null
+++ b/actionview/lib/action_view/helpers/tags/color_field.rb
@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+
+module ActionView
+ module Helpers
+ module Tags # :nodoc:
+ class ColorField < TextField # :nodoc:
+ def render
+ options = @options.stringify_keys
+ options["value"] ||= validate_color_string(value)
+ @options = options
+ super
+ end
+
+ private
+
+ def validate_color_string(string)
+ regex = /#[0-9a-fA-F]{6}/
+ if regex.match?(string)
+ string.downcase
+ else
+ "#000000"
+ end
+ end
+ end
+ end
+ end
+end