ruby-on-railsvalidationbefore-saveserver-side-validation

ActiveRecord::StatementInvalid (PG::InvalidDatetimeFormat: ERROR: invalid input syntax for type date: ""


In my model, I have:

validates_presence_of :start_date, :message => 'Please enter date.'
validate :start_date_exist_check , on: :create`

def start_date_exist_check 
  @check_date = Employee.where('start_date = ?', start_date)`
  if @check_date.blank?
  end
end

validate_presence_of is executed after my module start_date_exist_check, which creates a problem.

Can anybody tell me how I can validate the field first?


Solution

  • First check whether the start date is blank or not , if its blank, return it from first line

    validates_presence_of :start_date, :message => 'Please enter date.'
    validate :start_date_exist_check , on: :create`
    
    def start_date_exist_check 
      return if start_date.blank?
      @check_date = Employee.where('start_date = ?', start_date)`
      if @check_date.blank?
      end
    end
    

    Not tested let me know if its not work