I am using authlogic for my user login functionality in my Rails app. But authlogic allows space in password and my application doesn't require this. So how can I do it?
Thanks!
You could use a regular expression to define your password rules.
validates_format_of :password, :with => /{regular expression here}/, :if => :require_password?, :message => "Must not contain any spaces!"
Related
hello I am building a rails application for the college community, the idea is that only students with a valid #college.edu email address can sign up for it.
There is a students table with college as its column
I have looked at the rails doc under validation, mostly it tells you how to validate length, presence, emptiness etc.
will this gem help me?
gem "validates_email_format_of", "~> 1.5.3"
I was reading up on email validation and it go into parsers, RFC 2822 and RFC 3696 ? is there a simpler way to go about it like regular expressions?
I'm not familiar with the validates_email_format_of gem, but the following example of the use of validates is documented in http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates
validates :email, :format => { :with => /\A([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create }
In my current ROR project I am using devise pluing for validation. In my change password form validation, I am using the following code in the user model
validates_presence_of :password, :password_confirmation
But I wants to validate is only for an action. I have a function in my user controller named update_password. I found the that I can assign the action as follows:
validates_presence_of :password, :password_confirmation, :on => :update_password
But its not working. Even if the password and password confirmation fields are empty, the form is submitted. Can anyone help me to solve how to set the validation only for a particular action. Will be a great help
Thanks a lot
You can use :validatable option implemented in devise.
Just add to your model
devise :validatable
And set validation options in your config/initializers/devise.rb file
# ==> Configuration for :validatable
# Range for password length. Default is 6..128.
config.password_length = 6..128
# Email regex used to validate email formats. It simply asserts that
# an one (and only one) # exists in the given string. This is mainly
# to give user feedback and not to assert the e-mail validity.
config.email_regexp = /\A[^#]+#[^#]+\z/
Another way is to use your own regexp validations. You can add to your model
validates :password, :format => { :with => /\A[a-zA-Z]+\z/, :message => "Only letters allowed" }
After that you can call #user.valid? in your controller to check that your user instance is correct.
There are many different ways to validate your model.
You can read more about validation and ActiveRecord callbacks in guides: http://guides.rubyonrails.org/active_record_validations_callbacks.html
If you're using devise, there's no need to check for that. It's handle for you by default.
I have a Rails 3 form, with data-remote => true.
The form field is handle, like a twitter handle. I want to add validation to ensure it's handle friendly, meaning, no spaces, or non-url friendly characters.
Where to start, do I build a customer validation for this?
Thanks
Looks like you want to use validates_format_of
class Product < ActiveRecord::Base
validates_format_of :handle, :with => /\A[a-zA-Z]+\z/, :message => "Only letters allowed"
end
Change the regular expression pattern to match your needs. See the Rails Guides on validation for more information.
Look into validates_each
validates_each :handle do |record,attribute,value|
# your validation code here, and you can record.errors.add().
end
In rails 3 the best practice will be using
validates :handle, :format => {:with => /\A[a-zA-Z]+\z/, :message => "Only letters allowed"}
i want to put validation in ruby on rails that my username should not contain special characters like !##$%^*()_-+=/<>?:'";.
Please tell me how can i implement it in my code.
Using your model validates_format_of as suggested by #Voyta
I am adding a regular expression in my example though :
validates_format_of :username, :with => /\A[a-zA-Z]+([a-zA-Z]|\d)*\Z/
Use validates_format_of (see comments there for regex examples):
validates_format_of :username, :with => /\Ayour_regex_here\Z/i
Is there a way to get Authlogic to validate the format of a password, for instance must contain at least one letter and at least one number? The omission of a validates_format_of_password_options method to be used in the acts_as_authentic config block seems to indicate that Authlogic has the opinion that one should not be imposing such a constraint on one's users.
I thought I would simply put in a normal ActiveRecord validates_format_of :password, but this means that a current_user object I build is inherently invalid, as I can't retrieve the plaintext password (and wouldn't be storing it in that object even if I could!). Upon detecting that my current_user is invalid, Rails or Authlogic (not sure which, since I'm fairly new to both) directs me to my 'edit user' page with a validation error for its password.
requires no monkey-patching, but not tied to any future Authlogic changes. Just add this to your User model:
validates_format_of :password, :with => /^(?=.\d)(?=.([a-z]|[A-Z]))([\x20-\x7E]){6,40}$/, :if => :require_password?, :message => "must include one number, one letter and be between 6 and 40 characters"
Of course you can alter the regex to suite your needs.
You can use the configuration options given by acts_as_authentic like so:
# Configuration is easy:
#
# acts_as_authentic do |c|
# c.my_configuration_option = my_value
# end
#
# See the various sub modules for the configuration they provide.
If you go to the modules in the gem you can see additional options they provide. For example if I want to change the default options of the password's length validation:
acts_as_authentic do |c|
c.merge_validates_length_of_password_field_options({:minimum => 3})
end
You can look inside the acts_as_authentic folder in your "(gems || plugins)/authlogic/acts_as_authentic/" directory for more options. Cheers!