Forum: Ruby on Rails how to perform client side validations in RoR?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Prashant T. (Guest)
on 2006-04-04 12:38
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
Bryan Buecking (Guest)
on 2006-04-04 12:50
(Received via mailing list)
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


<div id="signup_form_area">
<%= 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:</label><br/>
        <%= password_field "user", "password_confirmation", :size => 30
%><br/>
        <input type="submit" value="add user&#187;" class="primary" />

    <%= end_form_tag %>
</div>

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

--
Bryan Buecking
Chris (Guest)
on 2006-04-04 14:14
What does this do exactly?
This topic is locked and can not be replied to.