2013-07-24 27 views
0

我正在創建表單,用於通過Ruby on Rails進行直接付款。如何從表單中傳遞值以進行驗證

我有表格設置,我已經設置了一些基本的驗證,以確保填寫所有字段。

接下來我想設置一些驗證來檢查比IBAN(國際銀行帳戶號碼)號碼是一個有效的IBAN。

這涉及到幾個步驟操縱一個字符串,改變它爲一個數字,並做一些數學,所有我已經寫了一個方法。

我的問題是,我似乎無法通過IBAN進行驗證的方法。

我的驗證:

class DirectDebit < ActiveRecord::Base 
attr_accessible :address, :amount, :bank_branch, :bic, :date_for_payment, :email, :iban, :name, :name_of_account, :phone 

validates :address, :bank_branch, :bic, :date_for_payment, :email, :name, :name_of_account, :phone, presence: true 

validates :amount, numericality: {greater_than_or_equal_to: 0.01} 

validate :iban 

def iban 

    ## Converts all letters in 'number' to uppercase and removes any spaces and anything else that isn't a letter, number or underscore 
    iban = iban.upcase.scan(/\w/).join 

    ## removes any underscores as ".scan(/\w/)" above reads in letters digits and underscores. 
    iban = iban.gsub(/_/, '') 

    ## Calculates the length of 'number' 
    iban_length = iban.length 

    ## Saves the first two letters in 'number' as 'country' 
    country = iban.scan(/\A../).join 

    ## Checks if the length is correct for the country 
    length_correct_for_country = true 

    case country 
     when "IE" 
      if iban_length == 22 
       length_correct_for_country = true 
      else 
       length_correct_for_country = false  
      end 
     when "AL" 
      if iban_length == 28 
       length_correct_for_country = true 
      else 
       length_correct_for_country = false  
      end 
     ... 
     when "GB" 
      if iban_length == 22 
       length_correct_for_country = true 
      else 
       length_correct_for_country = false  
      end 
     when "VG" 
      if iban_length == 24 
       length_correct_for_country = true 
      else 
       length_correct_for_country = false  
      end 
    end 

    ## Identifies what the first 4 characters are, and removes them from the rest of the number 
    first_four_characters = iban.slice!(0..3) 

    ## Adds the first four characters to the end of the rest 
    reordered_number = iban + first_four_characters 

    ## Set up an array and have each character in the reordered number read into it, changing it to the appropriate number if it is a letter. 
    letters_removed = [] 
    reordered_number.scan(/./) do |character| 
     case character 
     when "A" 
      letters_removed << 10 
     ... 
     when "9" 
      letters_removed <<9 
     end 
    end 

    ## Change the array to a String and then to a number 
    letters_removed = letters_removed.join.to_i 

    ## Check to see if it gives a remainder of one when divided by 97 
    remainder = letters_removed % 97 

    ## Output that it is a valid number if the remainder when it is divided by 97 is 1 and if it is the correct length for the country. 
    if remainder == 1 && length_correct_for_country 

    else 
     remainder = remainder.to_s 
     errors.add(:iban, " That is not a valid IBAN. The IBAN that is being supplied is: " + special) 
    end 

end 

end 
+0

在第一行嘗試'iban = self.iban.upcase.scan(/ \ w /)。join'。另外如果您不確定什麼是使用調試器的值,或者只是將「Rails.logger.info」*「* 50; Rails.logger.info [:iban_after_smth,iban] .inspect' – Lucas

回答

1

在您的模型中,iban已經是一種方法,因爲它是一個數據庫列。所以,你想要做的事,如:

validate :valid_iban? 

def valid_iban? 
    ## all of your current validation iban goes here 
end 

如果您希望能夠驗證這兩個模型的IBAN,以及潛在的其他伊班人:

應用程序/模型/ validates_iban.rb

class ValidatesIban 
    def self.valid?(iban) 
    ## all your validation goes here 
    end 
end 

,並在你的模型:

validate :valid_iban? 

def valid_iban? 
    unless ValidatesIban.valid?(iban) 
    self.errors(:iban, "is not valid") 
    end 
end 

的ValidatesIban方法的好的一面:您可以重新在別處使用。

+0

謝謝,ValidatesIban方法一旦有了它,我就有了更多的工作。 – ABetterNameThanAutoGenerated

0

試試這個:

class YourModel < ActiveRecord::Base 

    validate :your_method 


    def your_method 
    #your stuff should return true or false   
    end 

end 

希望這將有助於。

+0

這是我擁有的基本版本。 – ABetterNameThanAutoGenerated

+0

請在這裏粘貼一些代碼。謝謝 –

+0

完成,有兩個長的重複部分,我切出了中間,並用剛讀的行代替... – ABetterNameThanAutoGenerated

相關問題