-1
我創建了一個簡單的銀行應用程序This is the Link用於學習目的。所有的功能是迷人的工作,但我要檢查驗證,如果用戶輸入負值,如-100,所以它顯示錯誤或警報消息。那麼如何執行。我想驗證一個文本框檢查負值像-100?
感謝提前:)
_form.html.erb
<%= form_for(@depositemoney) do |f| %>
<% if @depositemoney.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@depositemoney.errors.count, "error") %> prohibited this depositemoney from being saved:</h2>
<ul>
<% @depositemoney.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :balance %><br>
<%= f.number_field :balance %>
</div>
<%= f.hidden_field :u_id, :value => current_user.id %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Model.rb
class Depositemoney < ActiveRecord::Base
# validates :balance, :numericality => {:only_integer => true}
# validates :balance, presence: true
validates :balance, presence: true
# validates :balance, :inclusion => {:in => [1,2]}
validates :balance, format: { with: /\A\d+\z/, message: "Integer only. No sign allowed." }
end
我嘗試驗證的3種類型,但它不worrk對我來說,任何人都可以幫助我:)
想必你'balance'場是一個數字,所以你要驗證與數值的比較,而不是字符串。你有沒有試過':0..1e9'作爲一個好範圍的例子? – tadman
我試過了1至9999的響鈴,但它不起作用。 –