Class: Bureaucrat::Fields::RegexField

Inherits:
CharField show all
Defined in:
lib/bureaucrat/fields.rb

Overview

DateField TimeField DateTimeField

Direct Known Subclasses

EmailField

Constant Summary

Constants included from Validation::Validators

Validation::Validators::EMAIL_RE

Instance Attribute Summary

Attributes inherited from CharField

#max_length, #min_length

Attributes inherited from Field

#error_messages, #help_text, #hidden_widget, #initial, #label, #required, #show_hidden_initial, #widget

Instance Method Summary collapse

Methods inherited from CharField

#widget_attrs

Methods inherited from Field

hidden_widget, inherited, #initialize_copy, set_error, #validating, widget, #widget_attrs

Methods included from Validation::Converters

to_big_decimal, to_bool, to_float, to_integer

Methods included from Validation::Validates

fail_with

Methods included from Validation::Validators

empty_value?, has_max_decimal_places, has_max_digits, has_max_length, has_max_whole_digits, has_min_length, included_in, is_array, is_email, is_not_greater_than, is_not_lesser_than, is_present, is_true, matches_regex, not_empty

Constructor Details

#initialize(regex, options = {}) ⇒ RegexField

Returns a new instance of RegexField.



259
260
261
262
263
264
265
266
267
# File 'lib/bureaucrat/fields.rb', line 259

def initialize(regex, options={})
  error_message = options.delete(:error_message)
  if error_message
    options[:error_messages] ||= {}
    options[:error_messages][:invalid] = error_messages
  end
  super(options)
  @regex = regex
end

Instance Method Details

#clean(value) ⇒ Object



269
270
271
272
273
274
# File 'lib/bureaucrat/fields.rb', line 269

def clean(value)
  value = super(value)
  return value if value.empty?
  validating { matches_regex(value, @regex) }
  value
end