ruby on rails - Override validations of gem devise -


in model user add following validations problem devise have implemented own validations. how can override validations of devise

user.rb

class user < activerecord::base   # include default devise modules. others available are:   # :token_authenticatable, :confirmable, :lockable , :timeoutable   devise :database_authenticatable, :registerable,          :recoverable, :rememberable, :trackable, :validatable    # setup accessible (or protected) attributes 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 blank  #validation of devise email email no puede estar vacio #my own validation password can't blank #validation of devise password password no puede estar vacio #my own validation password confirmation validate confirmation no puede estar vacio 

other problem have rails shows name of attribute before of customized message

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

you using :validatable devise module...

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

# ==> configuration :validatable # range password length. config.password_length = 8..128  # email regex used validate email formats. asserts # 1 (and one) @ exists in given string. # give user feedback , not assert e-mail validity. # config.email_regexp = /\a[^@]+@[^@]+\z/ 

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

in other words have 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 can override validation messages using rails i18n api part 4

in simpler words... have provide locale files preferred language (:es) in config/locales/es.rb providing 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 have field failed validation there.

note: if looking provide locale devise error messages (say :es locales)...

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


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -