class Formbuilder::EntryValidator
Constants
- SHARED_VALIDATION_METHODS
Public Instance Methods
validate(record)
click to toggle source
# File lib/formbuilder/entry_validator.rb, line 5 def validate(record) @record = record # I guess it's valid if there's no form? return if record.form.blank? # we can also skip validation by setting this flag return if record.skip_validation record.form.show_blind = true if record.only_validate_page query = record.form.response_fields_for_page(record.only_validate_page) else query = record.form.filtered_response_fields end query = query.reject { |rf| !rf.input_field } query.each do |response_field| @response_field = response_field @value = @record.response_value(@response_field) if @response_field.required? && !@record.value_present?(@response_field) add_error "can't be blank" next end if @record.value_present?(@response_field) # Field-specific validation add_error(@response_field.validate_response(@value)) SHARED_VALIDATION_METHODS.each do |method_name| run_validation(method_name) end end end end
Private Instance Methods
add_error(msg)
click to toggle source
# File lib/formbuilder/entry_validator.rb, line 45 def add_error(msg) return unless msg.present? @record.errors["#{@record.responses_column}_#{@response_field.id}"] << msg end
integer_only()
click to toggle source
# File lib/formbuilder/entry_validator.rb, line 107 def integer_only if @response_field.field_options["integer_only"] && !(Integer(@value) rescue false) "only integers allowed." end end
min_max()
click to toggle source
# File lib/formbuilder/entry_validator.rb, line 87 def min_max return unless @response_field.field_options["min"].present? || @response_field.field_options["max"].present? value_for_comparison = case @response_field.field_type when 'price' "#{@value['dollars'] || 0}.#{@value['cents'] || 0}".to_f else @value.to_f end if @response_field.field_options["min"].present? && (value_for_comparison < @response_field.field_options["min"].to_f) return "is too small. It should be #{@response_field.field_options["min"]} or more." end if @response_field.field_options["max"].present? && (value_for_comparison > @response_field.field_options["max"].to_f) return "is too large. It should be #{@response_field.field_options["max"]} or less." end end
min_max_length()
click to toggle source
# File lib/formbuilder/entry_validator.rb, line 56 def min_max_length return unless @response_field.field_options["minlength"].present? || @response_field.field_options["maxlength"].present? if @response_field.field_options["min_max_length_units"] == 'words' min_max_length_words else min_max_length_characters end end
min_max_length_characters()
click to toggle source
# File lib/formbuilder/entry_validator.rb, line 67 def min_max_length_characters if @response_field.field_options["minlength"].present? && (@value.length < @response_field.field_options["minlength"].to_i) return "is too short. It should be #{@response_field.field_options["minlength"]} characters or more." end if @response_field.field_options["maxlength"].present? && (@value.length > @response_field.field_options["maxlength"].to_i) return "is too long. It should be #{@response_field.field_options["maxlength"]} characters or less." end end
min_max_length_words()
click to toggle source
# File lib/formbuilder/entry_validator.rb, line 77 def min_max_length_words if @response_field.field_options["minlength"].present? && (@value.scan(/\w+/).count < @response_field.field_options["minlength"].to_i) return "is too short. It should be #{@response_field.field_options["minlength"]} words or more." end if @response_field.field_options["maxlength"].present? && (@value.scan(/\w+/).count > @response_field.field_options["maxlength"].to_i) return "is too long. It should be #{@response_field.field_options["maxlength"]} words or less." end end
run_validation(method_name)
click to toggle source
# File lib/formbuilder/entry_validator.rb, line 50 def run_validation(method_name) if (error_message = send(method_name)) add_error(error_message) end end