39 lines
1.1 KiB
Ruby
39 lines
1.1 KiB
Ruby
|
class AnswerFormatValidator < ActiveModel::EachValidator
|
||
|
def validate_each(record, attribute, value)
|
||
|
send(record.question.input_type, record, attribute, value)
|
||
|
end
|
||
|
|
||
|
private
|
||
|
|
||
|
def text record, attribute, value
|
||
|
return unless value.length.between? 1, 1000
|
||
|
|
||
|
if value.blank?
|
||
|
record.errors[attribute] << (options[:message] || "Please enter an answer.")
|
||
|
end
|
||
|
|
||
|
if value.length > 1000
|
||
|
record.errors[attribute] << (options[:message] || "The character limit for a textarea answer is 1000")
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def radio record, attribute, value
|
||
|
return unless value.nil?
|
||
|
|
||
|
record.errors[attribute] << (options[:message] || "Please select an answer.")
|
||
|
end
|
||
|
|
||
|
def checkbox record, attribute, value
|
||
|
return unless value.nil? || value.join.blank?
|
||
|
|
||
|
record.errors[attribute] << (options[:message] || "Please select an answer.")
|
||
|
end
|
||
|
|
||
|
def live_code record, attribute, value
|
||
|
return unless value.nil?
|
||
|
|
||
|
msg = "You must write code in one of the above textareas to progress."
|
||
|
record.errors[attribute] << (options[:message] || msg)
|
||
|
end
|
||
|
end
|