Commit 88fb8ffc by 李福中

Replace error message from english to chinese

parent be52d8da
...@@ -95,7 +95,7 @@ module RailsParam ...@@ -95,7 +95,7 @@ module RailsParam
end end
return nil return nil
rescue ArgumentError rescue ArgumentError
raise InvalidParameterError, "'#{param}' is not a valid #{type}" raise InvalidParameterError, error_message_for(nil, "#{param}不是合法的:#{type}", options)
end end
end end
...@@ -103,9 +103,9 @@ module RailsParam ...@@ -103,9 +103,9 @@ module RailsParam
options.each do |key, value| options.each do |key, value|
case key case key
when :required when :required
raise InvalidParameterError, "Parameter is required" if value && param.nil? raise InvalidParameterError, error_message_for(value, "不能为空", options) if value && param.nil?
when :blank 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 when String
!(/\S/ === param) !(/\S/ === param)
when Array, Hash when Array, Hash
...@@ -114,28 +114,36 @@ module RailsParam ...@@ -114,28 +114,36 @@ module RailsParam
param.nil? param.nil?
end end
when :format when :format
raise InvalidParameterError, "Parameter must be a string if using the format validation" unless param.kind_of?(String) raise InvalidParameterError, error_message_for(value, "格式不正确", options) unless param.kind_of?(String)
raise InvalidParameterError, "Parameter must match format #{value}" unless param =~ value raise InvalidParameterError, error_message_for(value, "必须匹配格式:#{value}", options) unless param =~ value
when :is 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 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 when Range
value.include?(param) value.include?(param)
else else
Array(value).include?(param) Array(value).include?(param)
end end
when :min 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 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 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 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 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
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