I have the following three validations in a model in my rails app:
validates_presence_of :reset_code, message: 'Please enter your reset code'
validates_length_of :reset_code, is: 4, message: 'Your reset code should be 4 digits'
validates_format_of :reset_code, with: /\A[0-9]{4}\z/, message: 'Please enter a valid reset code'
I only want to fire the second and third validations if the first is valid (as there is no point in telling the user the reset code isn't the correct length or format if they haven't entered one at all).
So something along the lines of:
validates_length_of :reset_code, is: 4, message: 'Your reset code should be 4 digits', :if => :reset_code.present?