【发布时间】:2011-02-03 21:25:53
【问题描述】:
Rails 显示与给定字段关联的所有验证错误消息。如果我有三个validates_XXXXX_of :email,并且将该字段留空,则会在错误列表中收到三条消息。
例子:
validates_presence_of :name
validates_presence_of :email
validates_presence_of :text
validates_length_of :name, :in => 6..30
validates_length_of :email, :in => 4..40
validates_length_of :text, :in => 4..200
validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i<br/>
<%= error_messages_for :comment %> 给我:
7 errors prohibited this comment from being saved
There were problems with the following fields:
Name can't be blank
Name is too short (minimum is 6 characters)
Email can't be blank
Email is too short (minimum is 4 characters)
Email is invalid
Text can't be blank
Text is too short (minimum is 4 characters)
最好一次显示一条消息。有没有简单的方法来解决这个问题?有这样一个条件看起来很简单:如果您发现 :email 有错误,请停止验证 :email 并跳到另一个字段。
【问题讨论】: