簡體   English   中英

在表單中驗證Rails中的多個字段

[英]Validate multiple fields in rails in the form

我的應用程序中有此表單,我想驗證表單的幾個字段。 即我想在表單提交期間將它們作為必填字段。

有什么方法可以使我不必為此任務使用任何簡單的好寶石?

<div class="col-md-4">
  <div class="form-group">
    <div class="field">
      <%= f.label :name %><br />
      <%= f.text_field :name, :class=>"form-control", :placeholder=>"Enter Your Name" %>
    </div>
    <div class="field">
      <%= f.label :email %><br />
      <%= f.text_field :email, :class=>"form-control", :placeholder=>"Enter Your Password"%>
      </div>
    <div class="field">
      <%= f.label :phone %><br />
      <%= f.text_field :phone, :class=>"form-control", :placeholder=>"Enter Your Phone Number"%>
      </div>
   </div> 
</div>

<div class = "col-md-8">
   <div class="form-group">
     <div class="field">
      <%= f.label :description %><br />
      <%= f.text_area :description,  :class=>"form-control", :size=>"20x5", :placeholder=>"Enter Your Message"%>
     </div>
   </div>
</div>

<div class="actions">
  <%= f.submit "Submit", :class=> "button1"%>
</div> 

提前致謝

我們可以提供用於客戶端驗證的各種插件。 您可以為此選擇jquery_validate。 請參閱: http : //jqueryvalidation.org/

但是客戶端驗證並不總是可靠的。 如果您禁用瀏覽器JavaScript,則驗證將不再起作用。

但是從用戶體驗的角度來看,請考慮實施客戶端驗證。 但是從應用程序安全性的角度來看,您還應該考慮進行服務器端驗證。

  validates :name, 
    presence: { message: "Organization name is mandatory." }

  validates :address, presence: {message: 'Address is mandatory.'}

  validates :description, presence: { message: 'Description is mandatory'}
  validates_length_of :name, 
    maximum: 50, too_long: "Maximum 50 characters allowed."

  validates_length_of :address, :description, 
    maximum: 300, too_long: "Maximum 300 characters allowed."

希望它能對您有所幫助:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM