The easiest way is probably to just define a custom method and use that for validation:
def registration_code_must_be_valid
# check if it's one of the hard coded values
unless [ 'code1', 'code2' ].include?(registration_code)
errors.add(:registration_code, "must be valid")
end
# or check if it's in another model if you want to do it dynamically
unless RegistrationCode.find_by_code(registration_code)
errors.add(:registration_code, "must be valid")
end
end
Another option is to create a custom validator. Check out that section of the guide: