juan perez
juan perez

Reputation: 323

Override validations of gem devise

In my model user I add the following validations The problem is that devise have implemented its own validations. How can I override the validations of Devise

user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable, :lockable and :timeoutable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me
  validates_presence_of :email, :message=>"email no puede estar vacio"
  validates_presence_of :password, :message=>"password no puede estar vacio"
  validates_presence_of :password_confirmation,:message=>"validate confirmation no puede estar vacio"
  end

Browser

Email can't be blank  #validation of devise
Email email no puede estar vacio #my own validation
Password can't be blank #validation of devise
Password password no puede estar vacio #my own validation
Password confirmation validate confirmation no puede estar vacio

Other problem that I have is that rails shows the name of attribute before of the customized message

Email email no puede estar vacio #my own validation #wrong
email no puede estar vacio #well

Upvotes: 0

Views: 1635

Answers (1)

a14m
a14m

Reputation: 8055

you are using the :validatable devise module...

you can customise the following lines in the confing/initializers/devise.rb

# ==> Configuration for :validatable
# Range for password length.
config.password_length = 8..128

# Email regex used to validate email formats. It simply asserts that
# one (and only one) @ exists in the given string. This is mainly
# to give user feedback and not to assert the e-mail validity.
# config.email_regexp = /\A[^@]+@[^@]+\z/

but if you are trying to override the error message (and solve the issue with the field name mentioned before your translation), you better remove your validations (in favour of devise ones)

in other words you have to remove these lines

validates_presence_of :email, :message=>"email no puede estar vacio"
validates_presence_of :password, :message=>"password no puede estar vacio"
validates_presence_of :password_confirmation,:message=>"validate confirmation no puede estar vacio"

and you can override the validation messages using the Rails I18n API specifically part 4

in simpler words... you have to provide the locale files for your preferred language (:es) in the config/locales/es.rb providing those specific translations

# please correct me if i'm wrong.
es:
  activerecord:
    errors:
      messages:
        record_invalid:
          email: email no puede estar vacio
          password: password no puede estar vacio
          password_confirmation: validate confirmation no puede estar vacio

but it's highly advisable to have the field that failed validation there.

note: if you are looking for just provide another locale for the devise error messages (say :es locales)...

then it's better to copy config/locales/devise.en.yml to config/locales/devise.es.yml and edit it accordingly... or use the devise wiki (github.com/plataformatec/devise/wiki/I18n)

Upvotes: 1

Related Questions