I have to check if confirm_password input box exists in form.If it exists,
I need to do this:
validates :password,:confirmation=>true
else
set confirmation to false.
Detail explanation for the problem:
I'm using rails client_side_validation gem which converts models validation into javascript form validation.
There is a little problem with this as login and sign up belongs to same table both of them have one Model. Now when I'm adding this in model for validation:
validates :password,:confirmation=>true
It will not let me to login as validation will become false as there is no confirm_password input box on login.It will only work on signup.
Assuming a model User add #user.is_signup = true to the signup action in users_controller. In your User model add attribute_accessor :is_signup and validates :confirm_password, :confirmation => true, :if => :is_signup.
In your signup form you could have a hidden field that gets passed in the form. If the attribute is present and returns true then you validate the presence of the password confirmation.
Signup form, somewhere inside the form tags:
<%= form.hidden_field :is_signup, true %>
Model:
attribute_accessor :confirm_password
attribute_accessor :is_signup
validates :confirm_password, :presence => true, :if => :validate_confirm_password?
def validate_confirm_password?
is_signup
end
Related
I have this validation for my user:
validates :password,
presence: true,
confirmation: true,
length: { minimum: 6 },
:on => :create
This is obvious. When I'm creating (registering) a new user, I want to fill up their password hence that's why presence: true.
Case 1) When the user wants to update his account (lets say change his username), the form has only the email and username fields. That's ok and the validation is ok.
Case 2) He forgot his password and I send him his "forgotten password link" and he is on the page where he is creating his new password. The form has these two fields: password and password confirmation. However, he leaves both of these fields empty and submits the form. The validation passes because it's only :on => create! because of case 1)
I can not add :on => update because the case 1) wouldn't pass, because there is no password field.
What should I do in this situation? What is the best practice or what is the real word solution to this "problem"?
What I have done for this situation is instead of using on: :create, I use a virtual attribute that I set only when setting/changing the password. Something like this:
validates :password, if: :changing_password?
attr_accessor :password_scenario
def changing_password?
self.password_scenario.present?
end
Then in your controller, you would simply set password_scenario to true whenever you are requiring password to be present.
I would like to simply validate a terms checkbox on my form. I have implemented the folowing:
http://guides.rubyonrails.org/active_record_validations.html#acceptance
class User < ActiveRecord::Base
validates :terms, acceptance: true
end
I have stripped the form back to the checkbox only for debugging purposes.
Regardless of the entry passed the :terms does not validate. The form parameters appear to being passed correctly.
Parameters: {"utf8"=>"✓", "authenticity_token"=>"+1dzwEMajQN4cL7KdGjlIw2kFSyVk/36eAhNhdydUXhLfzyT7LnCiUGdfzYt3hD/dD7evIVMiVWePv+7p+scyA==", "user"=>{"terms"=>"1"}, "commit"=>"Register"}
When I update the validation to the following I receive an error stating "Terms must be accepted" regardless of the terms value submitted. This leads me to believe the value from the form is not being passed to the validation.
validates :terms, acceptance: true, :allow_nil => false
OK...
The problem was that the validation helper validates the contents of the row within the table before saving. For this to trigger I needed to assign the ":terms" value to the model for validation.
I was only passing the params hash with the "terms" item within it expecting that to be validated.
I am working on a project and need some help on where to begin. I have three pages
Update User
Create User
Admin User Password Change (like a Hard Reset Password for but only the admin can reset the user's password)
Change Password
On Create User first name, last name, username, password, and password confirmation are mandatory.
On Update User just first name, last name and username are mandatory.
On Admin User Password Change and Change Password, just password and password confirmation are mandatory.
How would you go about doing this? I don't think this is possible through models using validates_presence_of with an if because there are too many scenarios. Any help or guidance would be appreciated. Also, I am pretty new to Rails if you can't already tell.
You can pass conditionals to your validations:
validates :password, :confirmation => true, :presence => true
validates :first_name, :last_name, :username, :presence => true
validate :admin_user_password_change?
Of course you'd have to define what the admin_user_password_change? method would be to determine if it is an admin user changing a password.
UPDATE
The admin_user_password_change? method might be something like:
def admin_user_password_change?
unless self.admin? && self.password.present? && self.password_confirmation.present?
self.errors.add(:admin_password_change, "password and password_confirmation are required.")
end
end
As for How would it communicate with the controller?, it wouldn't directly. But if any of the conditions in the method are false (e.g. self.admin? && self.password.present? && self.password_confirmation.present?), an error will be added to the instance of User and the instance won't save in the controller.
Setting some fields to new values doesn't unset other fields; just because you're only updating some fields in one action doesn't mean the other fields will be unset, so long as they start in a consistent state.
Just add your validations. It will work fine.
You can tell to your validation work only on certain cenarios only using:
The create:
validates :first_name, :last_name, :username, presence: true, on: :create
The update:
validates :password, presence: true, on: :update
Take a look at on.
For validation based on context take a look at Context Validations
In controller action I have two parameters: params[:name] and params[:email]. I would like check them. if they not null and haven't some wrong symbols. Why I asking - because I am not sure can I use validation in controller and I don't know how check param for some symbols...use regular function?...Here is my try:
Controller:
validates :name, :presence => true
validates :email, :presence => true
def check
name = params[:name].valid?
email = params[:email].valid?
end
The validation should occur in the model. Remember the controller should only translate HTTP requests into your application actions.
When you are creating an object you are doing it at the Model layer, so the validation should be at this layer, the model.
I have an User model. It has next fields:
attr_accessible :user_name, :first_name, :last_name, :email ....
There is a profile view for the User with 6 blocks. Each of them associated with the various fields. Box 1 - first_name and last_name, Box 2 - user_name and email, etc.
I need to validate all the fields (presence, format, etc). But validators must trigger only for those fields, that has came from a particular block (Box 1 or Box 2, for example).
If I write something like next:
validates :user_name, :presence => true
and I will not edit the block with the *user_name*, I will see the error "user Name can't be blank". I can't use *:allow_blank => true* or nil because it can't(!) be blank!
In two words: I must validate only those fields, that was past from the resquest.
What I can do to solve my problem? Thx
You can add if or unless option to skip of particular condition.
validates :user_name, :presence => true, :if => "first_name.blank? and last_name.blank?"
You can pull the specific fields out of your model and create a model for each block, then you add one_to_one relationships to your User model.