0

I'm trying to add password length validations to a form that creates a model, the artist. The form has three fields: name, email, and password. There is no password confirmation field. I added validations to all three fields. Both name and email validations return the respective errors when I fill in those fields wrong and both gives no errors when the fields are filled in correctly. However no matter what length of a password I fill in for the password field, it always gives an error "password is too short."

code so far for validations

File: Artist.rb

class Artist < ActiveRecord::Base
# attr_accessible :artist_name, :route_name

include UsersHelper

has_many :band_members
has_many :users, through: :band_members
has_many :events
has_many :performers
has_many :soundcloud_embeds
has_many :petitions

has_secure_password #validations: false

validate :should_not_have_profanities
validates :artist_name, presence: true
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: {          case_sensitive: false }
validates :password, length: 6..50
before_create { generate_route_name if route_name.blank? }
before_save { |user| user.email = email.downcase }

What I tried so far:

switching :password to :password_digest. This didn't work because It's password digest != password.

adding 'on: create' to 'validates :password'. This didn't seem to have any effect on fixing the problem.

6
  • I dont think within is required. Give this a try: validates :password, length: 6..50
    – Arctodus
    Jul 15, 2014 at 17:26
  • I removed the within, the problem is still the same as before
    – riv
    Jul 15, 2014 at 17:50
  • Ok, I dont see anything else that's obviously wrong here. Have you verified that the password is actually coming through and is being set on the object in the controller layer?
    – Arctodus
    Jul 15, 2014 at 17:59
  • yes after checking the console, the object is not being created because of the validations.
    – riv
    Jul 15, 2014 at 18:41
  • Can you post the params hash from the request? Jul 15, 2014 at 19:37

2 Answers 2

0

I think it should just be in, not within...

validates :password, length: { in: 6..50 }

See here: http://guides.rubyonrails.org/active_record_validations.html#length

3
  • Are you using strong_parameters and allowing the password attribute? Jul 15, 2014 at 17:53
  • do you mean by having this method in the object controller? def artist_params params.require(:artist).permit(:artist_name, :route_name, :password, :email, :background_image) end
    – riv
    Jul 15, 2014 at 18:45
  • yeah, and then your update action is using artist_params instead of params[:artist]? Jul 15, 2014 at 18:49
0

I often like using Regex for character validation. Difficult to learn but helps a lot. For a string length, you'll need something in your file like this:

VALID_PASSWORD_REGEX = /[a-zA-Z0-9]{6,16}/

validates format: { with: VALID_PASSWORD_REGEX }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.