2015-05-22 69 views
0

我有一個導軌形式建設者number_field如下:爲什麼Rails number_field表單生成器接受浮動值?

= f.number_field :integer_value, :step => 1, :class => 'form', :value => obj.value 

它生成的HTML爲:

<input class="form" id="people_attributes_232322900500_integer_value" name="people[people_attributes][232322900500][integer_value]" placeholder="" step="1" type="number"> 

但仍須接受浮點值。

PARAMS,我在服務器端獲取是

{........, "integer_value"=>"12123.323"} 
+0

在哪個瀏覽器中測試? – lcguida

+1

取決於瀏覽器 – apneadiving

+0

我在Safari中測試此功能 – Abhi

回答

1

這不是因爲鐵軌。 input type ='number'是一個HTML5結構,它只提供在該字段中提供的數字。它不限制用戶輸入非數字字符。

雖然大多數瀏覽器會在此字段上應用客戶端驗證,並且當您嘗試提交表單驗證錯誤時,會顯示。 注:驗證錯誤的瀏覽器和瀏覽器版本完全取決於

你應該申請現場服務器端驗證,以確保非數字字段不接受zwippie服務器端驗證

參考鏈接提供 檢查答案:http://www.w3schools.com/html/tryit.asp?filename=tryhtml_input_number

2

你仍然需要驗證在後端的數字,例如:

validates :integer_value, 
    :presence => true, 
    :numericality => { :only_integer => true, :greater_than => 0, :less_than_or_equal_to => 100 } 

對於輸入元素中,添加min="0"限制整數的值。不過,用戶可以在所有/大多數瀏覽器中輸入浮點數。

+1

很好的答案。我想補充一下:步驟只是指定當用戶點擊+/-按鈕時增加/減少值的量。它不會將值強制轉換爲整數。 – max

相關問題