如何在rails上创建facebook风格的单行验证?

时间:2013-12-06 23:25:56

标签: ruby-on-rails facebook validation

我喜欢facebook风格的单行验证,所以我写了这段代码:

def fields_filled_in?
  if first_name.blank? || last_name.blank? || email.blank?
    self.errors[:base] << "Please fill in all fields"
  else
    if email !~ /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
      self.errors[:base] << "Enter a valid email address"
    else
      if((first_name.length < 2 || last_name.length < 2) ||(first_name.length > 32 || last_name.length > 32))
        self.errors[:base] << "Enter valid full name" 
      else
        if password.blank?
          self.errors[:base] << "Enter a password"
        else
          if password.length < 6
            self.errors[:base] << "Password must be at least 6 character"
          end    
        end            
      end    
    end        
  end      
end

它只是工作,它缺少电子邮件的唯一性,看起来不像rails代码。

0 个答案:

没有答案