How to add custom errors to the User errors collection?

前端 未结 4 1292
南方客
南方客 2021-01-31 02:59

How can I add errors to the User model manually?

Is it just @user.errors << \"some text goes here\" ?

相关标签:
4条回答
  • 2021-01-31 02:59

    WARNING

    If you just add errors in a separate method (not in a validation), then by default when you call .valid? or .save those errors will be automatically cleared. So you may want to use validation contexts instead.

    0 讨论(0)
  • 2021-01-31 03:04
     @user.errors.add(:email, "Not valid")
    

    If you don't want to use any attributes, then in your model

     @user.errors[:base] << "This person is invalid because ..."
    

    For details: link

    0 讨论(0)
  • 2021-01-31 03:15

    try this:

    errors.add(:base, "#{user.full_name} has errors here!")
    
    0 讨论(0)
  • 2021-01-31 03:24

    Use the errors.add method

    Example:

    @user.errors.add(:name, "wasn't filled in")
    
    0 讨论(0)
提交回复
热议问题