How to perform client side validations in RoR?


#1

Hi,

I have a form for user registration. In that form there are fields for
password & confirm_password. Now in my database there is column
corresponding to password field. Now at clicnt side I want to validate
equality of password & confirm_password field. How to do that?
I have downloaded one password validator plugin & tried to used it. But
it requirs two seperate fields in database for password & confirm
passowrd. & I don’t want field for confirm_password in database. How to
do that?
can we use Some client side validations here? If yes …how? please tell
me.
Thanx in advance.
Prash


#2

Prashant T. wrote:

I have a form for user registration. In that form there are fields for
password & confirm_password. Now in my database there is column
corresponding to password field. Now at clicnt side I want to validate
equality of password & confirm_password field. How to do that?
I have downloaded one password validator plugin & tried to used it. But
it requirs two seperate fields in database for password & confirm
passowrd. & I don’t want field for confirm_password in database. How to
do that?
can we use Some client side validations here? If yes …how? please tell
me.
You might want to look up validates_confirmation_of

ie.

db users table id, login, password

class User < ActiveRecord::Base
validates_confirmation_of :password
end

Your controller could be something like:

class UserController < ApplicationController

def signup
@user = User.new(params[:user])
if @user.save
render_partial ‘signup_success’
else
render_partial ‘signup’, @user
end
end

and in your view

<%= form_remote_tag :update => 'signup_form_area', :url => { :controller => 'user_controller', :action=> 'signup' }%>
    <% if !@user.nil? %>
        <%= error_message_on 'user', 'login', 'User ' %>
    <% end %>
    <label for="user_login">username:</label><br/>
    <%= text_field "user", "login", :size => 30 %><br/>

    <% if !@user.nil? %>
        <%= error_message_on 'user', 'password', 'Password ' %>
    <% end %>
    <label for="user_password">password:</label><br/>
    <%= password_field "user", "password", :size => 30 %><br/>

    <label for="user_password_confirmation">confirm

password:

<%= password_field “user”, “password_confirmation”, :size => 30
%>

<%= end_form_tag %>

NOTE: this implementation uses Ajax, but you could easily do w/o.


Bryan Buecking


#3

What does this do exactly?