Rails: Validating min and max length of a string but allowing it to be blank

后端 未结 9 1845
清歌不尽
清歌不尽 2021-01-30 19:14

I have a field that I would like to validate. I want the field to be able to be left blank, but if a user is entering data I want it to be in a certain format. Currently I am us

9条回答
  •  悲哀的现实
    2021-01-30 19:49

    validates_length_of :reason, minimum: 3, maximum: 30
    

    rspec for the same is

    it { should validate_length_of(:reason).is_at_least(3).is_at_most(30) }
    

提交回复
热议问题