Commit 88fb8ffc by 李福中

Replace error message from english to chinese

parent be52d8da
......@@ -95,7 +95,7 @@ module RailsParam
end
return nil
rescue ArgumentError
raise InvalidParameterError, "'#{param}' is not a valid #{type}"
raise InvalidParameterError, error_message_for(nil, "#{param}不是合法的:#{type}", options)
end
end
......@@ -103,9 +103,9 @@ module RailsParam
options.each do |key, value|
case key
when :required
raise InvalidParameterError, "Parameter is required" if value && param.nil?
raise InvalidParameterError, error_message_for(value, "不能为空", options) if value && param.nil?
when :blank
raise InvalidParameterError, "Parameter cannot be blank" if !value && case param
raise InvalidParameterError, error_message_for(value, "不能为空", options) if !value && case param
when String
!(/\S/ === param)
when Array, Hash
......@@ -114,28 +114,36 @@ module RailsParam
param.nil?
end
when :format
raise InvalidParameterError, "Parameter must be a string if using the format validation" unless param.kind_of?(String)
raise InvalidParameterError, "Parameter must match format #{value}" unless param =~ value
raise InvalidParameterError, error_message_for(value, "格式不正确", options) unless param.kind_of?(String)
raise InvalidParameterError, error_message_for(value, "必须匹配格式:#{value}", options) unless param =~ value
when :is
raise InvalidParameterError, "Parameter must be #{value}" unless param === value
raise InvalidParameterError, error_message_for(value, "必须等于:#{value}", options) unless param === value
when :in, :within, :range
raise InvalidParameterError, "Parameter must be within #{value}" unless param.nil? || case value
raise InvalidParameterError, error_message_for(value, "必须包含于:#{value}", options) unless param.nil? || case value
when Range
value.include?(param)
else
Array(value).include?(param)
end
when :min
raise InvalidParameterError, "Parameter cannot be less than #{value}" unless param.nil? || value <= param
raise InvalidParameterError, error_message_for(value, "不能小于:#{value}", options) unless param.nil? || value <= param
when :max
raise InvalidParameterError, "Parameter cannot be greater than #{value}" unless param.nil? || value >= param
raise InvalidParameterError, error_message_for(value, "不能大于:#{value}", options) unless param.nil? || value >= param
when :min_length
raise InvalidParameterError, "Parameter cannot have length less than #{value}" unless param.nil? || value <= param.length
raise InvalidParameterError, error_message_for(value, "长度不能小于:#{value}", options) unless param.nil? || value <= param.length
when :max_length
raise InvalidParameterError, "Parameter cannot have length greater than #{value}" unless param.nil? || value >= param.length
raise InvalidParameterError, error_message_for(value, "长度不能大于:#{value}", options) unless param.nil? || value >= param.length
end
end
end
def error_message_for(value, message, options)
if options[:message].present?
return options[:message]
else
"#{options[:label]}#{message}" % value
end
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment