2012-04-09 26 views
12

Đây là những gì tôi mong đợi là một câu hỏi hoàn toàn đơn giản, nhưng tôi không thể tìm thấy câu trả lời cuối cùng trong Hướng dẫn hoặc ở nơi khác.Rails 3 Xác thực: presence => false

Tôi có hai thuộc tính trên ActiveRecord. Tôi muốn chính xác một người có mặt và người kia là nil hoặc một chuỗi trống.

Làm cách nào để thực hiện tương đương với: presence => false? Tôi muốn chắc chắn rằng giá trị là không.

validates :first_attribute, :presence => true, :if => "second_attribute.blank?" 
validates :second_attribute, :presence => true, :if => "first_attribute.blank?" 
# The two lines below fail because 'false' is an invalid option 
validates :first_attribute, :presence => false, :if => "!second_attribute.blank?" 
validates :second_attribute, :presence => false, :if => "!first_attribute.blank?" 

Hoặc có lẽ có một cách thanh lịch hơn để làm điều này ...

Tôi đang chạy Rails 3.0.9

+0

Tôi không chắc chắn bạn cần có: sự hiện diện => sai chút nào trong hai dòng cuối cùng của mã . – creativetechnologist

+0

@creativetechnologist Nó cần một thử nghiệm của một số loại. Nếu tôi thoát khỏi sự xác thực hiện diện: nó cho tôi: C: /Ruby192/lib/ruby/gems/1.9.1/gems/activemodel-3. 0.9/lib/active_model/validations/validates.rb: 79: trong 'validates ': Bạn cần cung cấp ít nhất một xác thực (ArgumentError) – LikeMaBell

+6

Đáng chú ý cho Rails 4 rằng điều này được gọi là' validates_absence_of'. – mpowered

Trả lời

8
class NoPresenceValidator < ActiveModel::EachValidator                                       
    def validate_each(record, attribute, value)         
    record.errors[attribute] << (options[:message] || 'must be blank') unless record.send(attribute).blank? 
    end                   
end  

validates :first_attribute, :presence => true, :if => "second_attribute.blank?" 
validates :second_attribute, :presence => true, :if => "first_attribute.blank?" 

validates :first_attribute, :no_presence => true, :if => "!second_attribute.blank?" 
validates :second_attribute, :no_presence => true, :if => "!first_attribute.blank?" 
0

Hãy thử:

validates :first_attribute, :presence => {:if => second_attribute.blank?} 
validates :second_attribute, :presence => {:if => (first_attribute.blank? && second_attribute.blank?)} 

Hy vọng rằng sẽ giúp đỡ.

1

Dường như: length => {: is => 0} hoạt động cho những gì tôi cần.

validates :first_attribute, :length => {:is => 0 }, :unless => "second_attribute.blank?" 
+1

Điều này có thông báo lỗi "là độ dài sai (phải là 0 ký tự)". Có thể thêm thông báo tùy chỉnh "phải để trống". 'xác thực: first_attribute,: length => {: is => 0,: message =>" phải để trống "},: trừ khi =>" second_attribute.blank? "' – tfentonz

3

sử dụng xác thực tùy chỉnh.

validate :validate_method 

# validate if which one required other should be blank 
def validate_method 
    errors.add(:field, :blank) if condition 
end 
23

Đối với cho phép một đối tượng có giá trị khi và chỉ khi một thuộc tính cụ thể là con số không, bạn có thể sử dụng "bao gồm" chứ không phải là tạo ra phương pháp riêng của mình.

validates :name, inclusion: { in: [nil] } 

này là dành cho Rails 3. Rails 4 giải pháp được nhiều hơn nữa tao nhã:

validates :name, absence: true 
Các vấn đề liên quan