An AuthLogic form is giving me incorrect validation errors — why?

五迷三道 提交于 2019-12-12 18:32:38

问题


I set up AuthLogic for Rails according to the AuthLogic example: http://github.com/binarylogic/authlogic_example.

I can log on successfully to the system, but when accessing users/new.html.erb to register a new user, the form returns the following validation errors:

Email is too short (minimum is 6 characters)
Email should look like an email address.
Login is too short (minimum is 3 characters)
Login should use only letters, numbers, spaces, and .-_@ please.
Password is too short (minimum is 4 characters)
Password confirmation is too short (minimum is 4 characters)

None of these errors exist in the data I am entering.

# new.html.erb
<%= form.label :login, nil, :class => "label" %><br />
<%= form.text_field :login, :class => "inputBox",
    :name => "login", :type => "text" %><br />

<%= form.label :password, form.object.new_record? ? nil : "Change password", :class => "label"  %><br />
<%= form.password_field :password, :class => "inputBox", 
    :name => "password", :type => "text" %><br />

<%= form.label "Confirm password", nil, :class => "label" %><br />
<%= form.password_field :password_confirmation, :class => "inputBox",
    :name => "password_confirmation", :type => "text" %><br />

<%= form.label :email, nil, :class => "label" %><br />
<%= form.text_field :email, :class => "inputBox",
    :name => "email", :type => "text" %><br />

# Users controller
def new
  @user = User.new
  render :layout => "forms"
end

I think the problem is that the data isn't being transferred somehow and therefore AuthLogic doesn't think the inputs are sufficient. Do you have any idea why AuthLogic is telling me the data doesn't satisfy its validation?

------MORE INFO------

# User model
class User < ActiveRecord::Base
  acts_as_authentic
  belongs_to :authenticable, :polymorphic => true
end

# Users controller, def create:
def create
    @user = User.new(params[:user])
  if @user.save
    flash[:notice] = "Account registered!"
    redirect_back_or_default account_url
  else
    render :action => :new
  end
end

回答1:


Check params[:user] and see if it's sending the correct values

You can also remove the validation in the model that acts_as_authentic adding the following fields:

acts_as_authentic do |c|
 c.validate_login_field = false
 c.validate_email_field = false
 c.validate_password_field = false
end



回答2:


Just a thought, do you have mass assignment disabled somewhere higher up than your model class?

I have had this happen before where I disabled mass assignment of attributes in my initializers, then forgot to set my model's attributes as accessible using attr_accessible for all required attributes inside the model class.




回答3:


One thing that is not clear in the documentation and in the videos, is that :password is still required in the attr_accessible line even though you may have crypted_password in the model (i.e. attr_accessible :password).

I'm not certain, but i'm assuming that authlogic uses this password variable to hold the clear text version before it encrypts it.

I don't recommend setting c.validate_password_field = false because this turns off encryption as well.



来源:https://stackoverflow.com/questions/2890652/an-authlogic-form-is-giving-me-incorrect-validation-errors-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!