Is there a way to return error code in addition to error message in rails active record validation?

后端 未结 3 1819
孤城傲影
孤城傲影 2021-02-05 16:04

In rails activerecord validation, normally if a validation fails, it will add an error message in the errors attribute of models, however our clients demands an error code be re

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-05 16:50

    errors is just a plain hash, with the key represents the attribute which has an error, and the value represents the error message. So technically your requirement is doable by replacing the text message with a hash. But the downside is you may need to do more things to show the errors in new format.

    For example, use a custom validator to add error code

    class Foo < ActiveRecord::Base
      attr_accessible :msiisnd
      validate :msiisdn_can_not_be_blank
    
      def msiisdn_can_not_be_blank
        if msiisdn.blank?
          errors.add(:msiisdn, {code: 101, message: "cannot be blank"})
        end
      end
    end
    

    Then use it

    foo = Foo.new
    foo.errors.count
    #=> 0
    foo.valid?
    #=> false
    foo.errors.count
    #=> 1
    foo.errors[:msiisdn]
    #=> [{ code: 101, message: "can not be blank"}]
    foo.errors[:msiisdn][0][:code]
    #=> 101
    

    So you can use it. But you need to do more work when you need to show the errors correctly, say displaying errors in a form, as this is not a convention.

提交回复
热议问题