Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using custom authlogic error messages

I am using the authlogic gem for user validation on one of my sites. All is going well, but I am wondering if it's possible to change the error message that gets returned when the user types in an invalid email address.

Thanks!

like image 937
bloudermilk Avatar asked Dec 16 '09 23:12

bloudermilk


2 Answers

authlogic has a special setting for this purpose:

class UserSession < Authlogic::Session::Base  
  generalize_credentials_error_messages true
end

The error message will be the same: "Email/Password combination is not valid", whether the password or email is bad. You can change the text of the message specifying a string instead of true:

generalize_credentials_error_messages "Try again"
like image 174
Braulio Carreno Avatar answered Oct 18 '22 16:10

Braulio Carreno


You can override the settings for email validation with validates_format_of_email_field_options. However, if you only want to change the message you can merge options with merge_validates_format_of_email_field_options so that only the options you specify are overridden. You specify settings in your User controller like so:

class User < ActiveRecord::Base
    acts_as_authentic do |c|
        c.merge_validates_format_of_email_field_options :message => 'My message'
    end
end

You can also change the settings for length and uniqueness validations. There are also a lot more other settings, take a look at the documentation, in the ::Config sections of each module you can find settings and their default values and how to override them.

Alternatively you can use localization and set error_messages.email_invalid (that's what the plugin looks for before setting it to the default English sentence, also useful if you are building an international application).

like image 22
Janteh Avatar answered Oct 18 '22 17:10

Janteh