Tag: validation

Rails添加错误:base不按预期工作

我有一个双嵌套forms: 当用户没有为user_item_image上传文件时,我需要显示错误消息。 我写了一个自定义validation: user_item_image.rb class UserItemImage < ActiveRecord::Base include PicturesHelper attr_accessor :foo mount_uploader :picture, PictureUploader belongs_to :user_item validate :picture_size validate :has_picture private def has_picture errors.add(:base, 'You must include at least one picture.') if picture.blank? end end 但我收到错误消息: 用户项目用户项目图像库您必须至少包含一张图片。 如何重写validation以使其不显示属性并仅显示消息。

在Rails 4中使用覆盖错误消息中的值作为自定义validation器

我在Rails 4.2上我编写了一个自定义Validator,它将检查输入的值是否存在于另一个表中。 我一直在审查其他一些post,似乎有一个特定于上下文或rails版本的首选方法来重用被validation的value 。 在rails文档中,我看到例如: validates :subdomain, exclusion: { in: %w(www us ca jp), message: “%{value} is reserved.” } 但是,如果我尝试在我的自定义消息覆盖中使用%{value} ,则它不会进行插值,而只是打印“%{value}”。 我见过各种称之为“价值”的方法。 我也无法让%{value} to work in my Validator definition, but could get #{value} to work (New to ruby, if #{value}从validate_each获取它?)。 我一直在努力处理各种格式的validation语句并输入自定义消息。 从文档看起来可重复的一些东西不是。 如果我声明自定义消息的方式导致错误,请告诉我如何更正? class ExistingGroupValidator value).any? record.errors[attribute] << (options[:message] || "#{value} is not a valid […]

如何在n条件下在ActiveRecord中进行条件validation?

我正在提供一个由外部公司调用的Web服务。 所需的数据涵盖了几个模型,包括人员,地址等。我想根据请求中的某些字段有条件地validation接收的数据。 我最终会有许多不同的validation数据集,虽然目前我只有一个,我即将添加第二个。 我目前的模型看起来像这样 class Person 1..32, :allow_blank => true … … end 从概念上讲,我的模型现在需要做这样的事情。 class Person 1..32 else if company_name == ‘DEF’ validates_length_of :first_name, :within => 2..20 end else if country == ‘DE’ if company_name == ‘ABC’ validates_length_of :first_name, :within => 1..32 else if company_name == ‘DEF’ validates_length_of :first_name, :within => 2..20 end end end […]

从活动记录错误视图页面更改validation错误以清除闪存消息

我在如何validation数组字段的成员中找到以下代码? 。 # Validates the values of an Enumerable with other validators. # Generates error messages that include the index and value of # invalid elements. # # Example: # # validates :values, enum: { presence: true, inclusion: { in: %w{ big small } } } # class EnumValidator before_errors prefix = “element #{@count} (#{value}) […]

如果validation失败,则显示注释错误

我有一个表单,登录用户可以在post中添加评论。 这是我的文件中的代码,到目前为止: # views/posts/show.html.haml %h3 This post has #{ pluralize(@post.comments.count, “comment”) } = render @post.comments # views/comments/_comment.html.haml = render “comments/form” # views/comments/_comment.html.haml – list all comments of the post .media .media-body %h4.media-heading = comment.user.name %small added #{time_ago_in_words comment.created_at} ago %p= comment.body %hr # views/comments/_form.html.haml – if user_signed_in? = errors_for @comment if @comment # errors_for is […]

更新配置文件而不更新密码

我希望能够更新我的用户信息,而无需在每次编辑任何其他属性时让用户设置密码。 我目前的validation是: validates :password, presence: true, length: { minimum: 8 } validates :password_confirm, presence: true 我怎么能让这个有条件? 如果密码和password_confirm属性在params中,那么只需要这些validation就很聪明。 我可以使用一些关于如何实现这一点的想法。 谢谢。

validation前删除所有html标记

有没有一种干净的方法来删除所有属性的所有html标签validation之前我发现acts_as_sanitized似乎是完美的,但对于rails 2:-s 谢谢

仅当virt_attribute为false时才validation字段

我有这样的模特: class Order {:message => I18n.t(:city_not_chosen)} validates :zip, :presence => {:message => I18n.t(:zip_not_chosen)} validates :street, :presence => {:message => I18n.t(:street__not_chosen)} validates :building, :presence => {:message => I18n.t(:building_not_chosen)} validates :phone_number, :presence => {:message => I18n.t(:phone_number_not_chosen)} validates :receiver, :presence => {:message => I18n.t(:receiver_not_chosen)} end 正如你所看到的,我在模型中设置了一些非db字段(use_user_data) – 虚拟属性… 但是怎么做,如果:use_user_data是假的,好的和正确的validation,但是当真的没有validation? 我试试这样: validates :city, :presence => {:message => I18n.t(:city_not_chosen)}, :unless […]

如果从特定路由调用创建方法,如何检查validation?

如果从其他路径调用创建方法,我想在创建方法上validation列存在。 例如,如果我有以下两条路线: post ‘create_item’, to: ‘item#create’ post ‘create_verified_item’, to: ‘item#create_verified’ 我需要在Item模型中定义这样的东西: validates :verified_number, presence: true, if: Item.action_name == “create_verified” 有人可以帮忙吗?

Rails 4:从自定义validation器中的错误消息中删除属性名称

在我的Rails 4应用程序中,我在Post模型上实现了一个名为LinkValidator的自定义validation器: class LinkValidator < ActiveModel::Validator def validate(record) if record.format == "Link" if extract_link(record.copy).blank? record.errors[:copy] << 'Please make sure the copy of this post includes a link.' end end end end 一切正常,但目前显示的信息是: 1 error prohibited this post from being saved: Copy Please make sure the copy of this post includes a link. 如何从上述消息中删除“复制”一词? 我在validation器中尝试了record.errors << […]