在我看來,我已經為輸入欄位添加了類似的驗證并且作業正常。還有另外兩個浮點欄位是 text_fields,當我使用與以前相同的邏輯時它不起作用。
.control-group
%label.control-label{ for: 'holding[lat]' }= t('Geo_location')
.controls
= f.text_field :lat, input_html: { step: '0.000001', max: 99.999999 }, placeholder: 'Max 99.999999'
%br
= f.text_field :lng, input_html: { step: '0.000001', max: 99.999999, min: -99.999999 }, placeholder: 'Max -99.999999'
還有其他方法可以做類似的事情嗎?
這是它適用的欄位 - = f.input :quantity_acres, label: t('Number_of_acres'), input_html: { step: '0.01', max: 999999.99 }, placeholder: 'Max 999999.99'
uj5u.com熱心網友回復:
只需在您的模型中添加此驗證
validates :lat, :lng, numericality: { greater_than_or_equal_to: -99.999999, less_than_or_equal_to: 99.999999 }
或者,如果您有不同的 MIN MAX 值lat并且lng您可以添加兩個驗證
validates :lat, numericality: { greater_than_or_equal_to: MIN_FOR_LAT, less_than_or_equal_to: MAX_FOR_LAT }
validates :lng, numericality: { greater_than_or_equal_to: MIN_FOR_LNG, less_than_or_equal_to: MAX_FOR_LNG }
用您的數字替換MIN_FOR_LAT, MAX_FOR_LAT, MIN_FOR_LNG,MAX_FOR_LNG
lat當和lng是字串時,它也可以完美地作業
對于發送表單之前的驗證,您可以使用這些示例之一,具體取決于您的表單構建器
simple_form
= simple_form_for :your_record do |f|
= f.input :lat, as: :integer, input_html: { step: 0.000001, max: 99.999999, min: -99.999999 }
= f.submit
form_for
= form_for :your_record do |f|
= f.number_field :lat, step: 0.000001, max: 99.999999, min: -99.999999
= f.submit
semantic_form
= semantic_form_for :your_record, html: { novalidate: false } do |f|
= f.inputs do
= f.input :lat, as: :number, step: 0.000001, max: 99.999999, min: -99.999999
= f.actions do
= f.action :submit
經測驗Chrome Version 100.0.4896.127 (Official Build) (x86_64)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/488881.html
