I try to add a validation from a blog category only limited at 1 word.
But I try this length: { maximum: 1 }
I doesn't work. Is there a validation to validaes only one word and not uniqueness?
Thank you for your answers
I try to add a validation from a blog category only limited at 1 word.
But I try this length: { maximum: 1 }
I doesn't work. Is there a validation to validaes only one word and not uniqueness?
Thank you for your answers
You can make a custom validation:
validates :category, uniqueness: true
validate :category_in_1_word
private
def category_in_1_word
if category.to_s.squish.split.size != 1
errors.add(:category, 'must be 1 word')
end
end
you can try:
validates :category, :format => { :with => /^[A-Za-z]+$/, :message => "Must be single word" }
No Rails doesn't have the validation you need, but you can easily create a custom one:
Try something like this:
class Post < ActiveRecord::Base
validate do
if ... # any custom logic goes here
errors.add :title, "is wrong"
end
end
end