验证一个空白域Rails模型

时间:2021-01-28 11:51:43

This may be somewhat basic but cannot find a definitive answer anywhere. I have set up a contact form within my app and have put in a hidden field that when completed disables the submit button with some Jquery. My attempt at stopping automated spam..

这可能有些基本,但在任何地方都找不到明确的答案。我已经在我的app中建立了一个联系人表单,并在一个隐藏的字段中,当完成后,用Jquery禁用提交按钮。我试图阻止自动垃圾邮件。

Can I also add some validations in my model?

我还可以在我的模型中添加一些验证吗?

validates :ghost, :presence => false

Looking at the docs this is invalid? I want the form to fail if this field is filled in. Not sure how to go about this one

看看医生,这是无效的?如果这个字段填充了,我希望表单失败。不知道该怎么做

EDIT

编辑

So I have now read that I could possibly use

现在我已经读到我可以用了。

 validates_exclusion_of :ghost, :on => :create

Though this is still failing as i dont think i am passing the correct arguments.

虽然这仍然是失败的,因为我认为我没有通过正确的论点。

3 个解决方案

#1


5  

:presence => false means that you disable presence validator.

:presence => false表示禁用存在验证器。

You need to write own absence validation (though in Rails 4.0 such validation exists, absence: true).

您需要编写自己的缺席验证(尽管在Rails 4.0中存在这样的验证,但缺席:true)。

validate :ghost_is_absent

def ghost_is_absent
  errors.add :ghost if ghost.present?
end

#2


1  

I am sorry to say , but why are you trying to do things so differently, doing it this way will make things more confusing for any future developer working on this piece of validation. First thing: 1) You can do the reverse of it , mark it as spam when the field is empty and vice versa and then simply check with the validation validates_presence_of :ghost 2)or if you want to protect spam use capcha (recapcha gem for that)

我很抱歉地说,但是为什么您要尝试以如此不同的方式做事情,以这种方式做将使任何未来的开发人员感到更加困惑。首先:1)您可以做相反的事情,当字段为空时将其标记为垃圾邮件,反之亦然,然后只需与验证验证器_presence_of:ghost 2进行检查)或如果您想保护垃圾邮件,请使用capcha (recapcha)

3) or if you want it do it your way only , then just add a custom validation Try creating a custom validation.

3)或者如果您希望它只按您的方式进行,那么只需添加一个自定义验证,尝试创建一个自定义验证。

validate :check_for_spam

def check_for_spam
    errors.add_to_base "ghost is present this is a spam" if ghost.present?
end

#3


0  

If you want to check if :ghost is blank:

如果要检查:ghost is blank:

  validates :ghost, inclusion: {in: ['']}

If you want to check if :ghost is nil, you have to rely to a custom validator.

如果您想检查:ghost是否为nil,您必须依赖自定义验证器。

#1


5  

:presence => false means that you disable presence validator.

:presence => false表示禁用存在验证器。

You need to write own absence validation (though in Rails 4.0 such validation exists, absence: true).

您需要编写自己的缺席验证(尽管在Rails 4.0中存在这样的验证,但缺席:true)。

validate :ghost_is_absent

def ghost_is_absent
  errors.add :ghost if ghost.present?
end

#2


1  

I am sorry to say , but why are you trying to do things so differently, doing it this way will make things more confusing for any future developer working on this piece of validation. First thing: 1) You can do the reverse of it , mark it as spam when the field is empty and vice versa and then simply check with the validation validates_presence_of :ghost 2)or if you want to protect spam use capcha (recapcha gem for that)

我很抱歉地说,但是为什么您要尝试以如此不同的方式做事情,以这种方式做将使任何未来的开发人员感到更加困惑。首先:1)您可以做相反的事情,当字段为空时将其标记为垃圾邮件,反之亦然,然后只需与验证验证器_presence_of:ghost 2进行检查)或如果您想保护垃圾邮件,请使用capcha (recapcha)

3) or if you want it do it your way only , then just add a custom validation Try creating a custom validation.

3)或者如果您希望它只按您的方式进行,那么只需添加一个自定义验证,尝试创建一个自定义验证。

validate :check_for_spam

def check_for_spam
    errors.add_to_base "ghost is present this is a spam" if ghost.present?
end

#3


0  

If you want to check if :ghost is blank:

如果要检查:ghost is blank:

  validates :ghost, inclusion: {in: ['']}

If you want to check if :ghost is nil, you have to rely to a custom validator.

如果您想检查:ghost是否为nil,您必须依赖自定义验证器。