我现在正试图在rails中开发api。我想显示字段电子邮件和.In字段的验证消息,我的模型我添加了下面的代码来验证电子邮件
validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i}, uniqueness: true
validates :email_confirmation, presence: true, if: :not_recovering_password, on: :create
validates :email, confirmation: true, email_format: {message: 'Invalid E-mail address'}, if: :not_recovering_password, on: :create对千人的验证
validates :houseno, presence: true, numericality: true, length: {minimum: 3, maximum: 3}但是当我给了一个空字符串给千人和电子邮件。它产生了多个验证错误。
"errors": {
"email": [
"Please enter your e-mail address",
"There is an error in the input value of"
],
"houseno": [
"Please enter a houseno",
"Please enter a numeric value is",
"Please enter three or more characters"
],如何才能获得第一条验证消息。请帮助解决这个问题。提前感谢
发布于 2016-10-26 07:14:13
您的模型有一个属性errors。此对象包含所有错误。如果您想要一个特定的错误,您可以使用object.errors[:email]来获取一个字段的错误数组。您可以通过object.errors[:email].first访问字段的第一个错误消息。您可以检查object.errors.include?(:email)中是否存在特定字段的错误。
https://stackoverflow.com/questions/40255545
复制相似问题