r2 - 11 May 2006 - 10:21:18 - PatipatTumsangthongYou are here: SETEC Wiki >  Knowledge Web  > WebTechnologyCategory > RubyOnRails > ValidationOnRails
-- JaruTangpoonpholwiwat - 22 May 2012 - 15:09

ValidationOnRails

validates_presence_of

เป็นการตรวจสอบ attributes ไม่ให้มีค่าว่าง

validates_presence_of attr... [options...]

validates_length_of

เป็นการตรวจสอบความกว้างค่าของ attributes

validates_length_of attr... [options...]


Options (validates_length_of)
:in (or :with)   range      ความกว้างของ length ต้องกำหนดเป็นช่วง
:is                 integer    ต้องมีค่าเท่ากับ integer ที่กำหนด
:minimum        integer    ต้องมีค่าไม่น้อยกว่าค่า integer ที่กำหนด
:maximum       integer    ต้องมีค่าไม่มากกว่าค่า integer ที่กำหนด
:message       text        เป็นการกำหนดข้อความที่จะแสดงเมื่อมีการตรวจสอบพบความผิดพลาดของ Validation
:on                             ตรวจสอบเมื่อมีการกระทำเหตุการร์ที่กำหนดไว้ คือ :save, :create, :update
:too_long       text        เป็นการกำหนดข้อความที่จะแสดงเมื่อมีการใช้ :maximum
:too_short      text        เป็นการกำหนดข้อความที่จะแสดงเมื่อมีการใช้ :minimum
:wrong_length text        เป็นการกำหนดข้อความที่จะแสดงเมื่อมีการใช้ :is

ตัวอย่างการใช้งาน

class Room < ActiveRecord::Base
  has_many :reservation
  validates_presence_of :name, :message => "กรุณากรอกชื่อห้องประชุม"
  validates_length_of :name, :maximum => 50, :message => "กรอกสูงสุด 50 ตัวอักษร"
  validates_presence_of :description, :message => "กรุณากรอกรายละเอียด"
  validates_length_of :description, :maximum => 255, :message => "กรอกสูงสุด 255 ตัวอักษร"
end
Edit | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r2 < r1 | More topic actions
 
Powered by SETEC Wiki
Copyright ©2012 by National Electronics and Computer Technology Center, NECTEC.
Ideas, requests, problems regarding SETEC Wiki? Send feedback