0

I created a Users controller with the following validation on the password :

validates :password, length: {minimum: 3, message: 'is too short, minimum of 3 characters'}, on: [:create, :update]

But in another controller, I perform this:

def create
  user_params = params.require(:user).permit(:email)
  @user = User.find_by_email(user_params[:email])

  if @user
    @user.regenerate_recover_password
    ...

And so I get the following error :

Validation failed: Password is too short, minimum of 3 characters

The basic idea was to have this validation because otherwise it is possible to use an empty password

Any idea ? Thank's

2 Answers2

0

Save

Ref: Rails skip validation within model with save?

.save(validate: false)

Update

You may have to use this inside your function. If you updating instead of saving: https://stackoverflow.com/a/9474881/1006936

object.attribute = value
object.save(:validate => false)

Skip validation in function

To skip update for your function: skip certain validation method in Model

class User < ActiveRecord::Base

  validates :password, length: {minimum: 3, message: 'is too short, minimum of 3 characters'}, on: [:create, :update]
, unless: :regenerate_recover_password
....
end
kawadhiya21
  • 2,458
  • 21
  • 34
  • I'm not sure I understand (I'm a Rails beginner) because I don't have a save method in my method. See full method code : https://zupimages.net/up/21/27/is02.png Something like : ``` @user.recover_password = random_string_gen @user.save(validate: false) ... ``` –  Jul 05 '21 at 17:26
  • Check now. Added that too. – kawadhiya21 Jul 05 '21 at 17:31
  • it enters an infinite loop and the application crashes with this :/ What I don't understand is that the generated string should pass the validation, if I give it a string manually of 4char or more it's ok –  Jul 05 '21 at 17:39
0

I think you may be approaching the problem wrong - instead of trying to limit your validations per controller use the if: and unless: options to limit when the password validation is enforced:

class User < ApplicationRecord
  validates :password, 
    length: { 
      minimum: 3, 
      message: 'is too short, minimum of 3 characters' 
    }, 
    confirmation: true,
    if: :password_required?

  private

  def password_required?
    new_record? || reset_token_sent?
  end

  def reset_token_sent?
    # ... @todo implement
  end
end

Note that these shouldn't be confused with the if/unless Ruby keywords. These are just options passed to the method.

max
  • 96,212
  • 14
  • 104
  • 165
  • On a side note - if your a beginner at Rails reinventing the authorization wheel isn't a great idea unless you're doing it as a learning experience. Use a battle tested solution instead like Devise instead. – max Jul 06 '21 at 08:29
  • Thanks for your answer, indeed I know it's not a good idea, it's only to test, I prefer to try by myself to understand better before using gems at all without knowing what it does :) –  Jul 06 '21 at 14:58