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

后端 未结 9 1849
清歌不尽
清歌不尽 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:56

    Or even more concise (with the new hash syntax), from the validates documentation:

    validates :foo, length: 5..5, allow_blank: true
    

    The upper limit should probably represent somehting more meaningful like "in: 5..20", but just answering the question to the letter.

提交回复
热议问题