.

How to use an auto_complete to validate passwords

As a follow up to my previous article about using auto_completes to ensure unique filenames, I’ve put together a bit of code that does essentially the same thing except that it checks a password field to ensure that it meets certain criteria.

  1. # view file
  2. <%= text_field_with_auto_complete :user, :password %><div id=login_error></div>
  3.  
  4. # model file
  5.  
  6. def User < ActiveRecord::Base
  7.   validates_length_of :password, :in=>3..20
  8. end
  9.  
  10. # controller file
  11.  
  12. def auto_complete_for_user_password
  13.   @user = User.new(params[:user])
  14.   @user.valid# ensures the errors array is populated
  15.   password_errors = @user.errors.on(‘password’)
  16.   render :update do |page|
  17.      page.replace_html ‘login_error’, (password_errors.empty? ? "" : password_errors)
  18.    end
  19. end

This will indicate when a password does not meet the validation criteria defined in the model. You can add custom validations to check the strength of the password itself.

A quick tip for searching for rails related information

If you use Firefox, you can use this little trick.

  1. go to Rails Google co-op
  2. right click on the search box.
  3. select ‘Add keyword for this search’
  4. fill out the resulting form and add a keyword like ‘rails’
  5. move focus to the address bar (CMD-L on a Mac)
  6. Type ‘rails sciwerks’ on the address bar

You can also do this with rails or ruby API methods.

You could also set up a quicksilver web search for this too.

Passing extra data with an auto_complete

I recently found myself trying to use an auto_complete field to allow the user to select an object from a list of possible objects. That’s all fine and dandy, but I wanted to exclude certain values depending on which object I was editing. There are at least two ways to do this, the first is to stuff the extra data into the session and then read that value from the auto_complete_for method and act appropriately.

I don’t really like this approach because I don’t want to clutter up the session with various form parameters. Thankfully, there is a simple way to do this by once again abusing the built in text_field_with_auto_complete function…

  1. # View
  2. <%= text_field_with_auto_complete ‘new_object’, ‘name’, { :tabindex=>1 }, {:url=>{:action=>’auto_complete_for_object_name’, :extra=>@object.extra} %>

[In my case, I used the :extra data to exclude certain objects from the auto_complete_for response]

By passing the :url option, you rewrite the URL that the AJAX actions call, enabling you to add extra parameters that can be acted on by the method. Also note that you can change the name of the action called (in fact, you need to specify it or it will default to the name of the action for the current view… probably not what you want). This is handy because it lets you assign the response to a variable with a different name without writing a separate auto_complete_for handler. In this case, the submitted form will have a ‘new_object[name]‘ in the params, even though the action called was ‘auto_complete_for_object_name’.

Needless to say, you need to write a custom ‘auto_complete_for’ method to handle the extra data. Note that you could also use this method to handle multiple auto_complete fields.

Next Page »