How to validate a aray in model?
In form I have a multiple select option, that returns a array
(in model I convert array to string and save it to database)
array can only have values [‘A’,‘B’,‘C’,‘D’,‘E’,’’]
So how can I valdate that some value is not a X or Y ?
Use a custom validator method that looks at the elements. See:
and use an array of bad elements with a method like so:
badElts = [:put, :bad, :element, :values, :here]
def validate record
record.array_of_stuff.each do |elt|
record.errors[:name] << “’#{elt}’ is forbidden” if
badElts.include? elt
end
end
-Dave
–
LOOKING FOR WORK! What: Ruby (on/off Rails), Python, other modern
languages.
Where: Northern Virginia, Washington DC (near Orange Line), and remote
work.
See: davearonson.com (main) * codosaur.us (code) * dare2xl.com
(excellence).
Specialization is for insects. (Heinlein) - Have Pun, Will Babble!
(Aronson)
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.