I'd like to validate a field with regex and also allow it to be blank (to accept an empty string). So far, the only thing I managed is to write a regex that allows an empty string, like:
validates :field,
format: { with: /\A([a-z]+|)\z/i }
Now, this can't be a proper way - this seems like an ugly hack. I'd like to know if there's another (proper) approach?
allow_blank should work. (There is also allow_nil for accepting nil values only (not an empty string))
validates :field,
format: { with: /\A([a-z]+|)\z/i }, :allow_blank => true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With