I''m wondering how you would go about putting a "confirm password" field into a form and not have it inserted into the database. An example would be on the Basecamp sign up form. And also on that page is an "accept terms" checkbox. I guess I want to know how do you run these fields through validation (model or controller) and not have them automatically entered into the database. Russ
I added extra fields called password_confirmation and email_confirmation to my signup form and added this to the controller : @newuser.errors.add("password", "does not match") unless @newuser.password == @params[''password_confirmation''] @newuser.errors.add("email", "does not match") unless @newuser.email == @params[''email_confirmation''] On Thu, 11 Nov 2004 12:35:34 -0800, Russ Smith <russ-Bff6KPpgmvsxnA8XTFIEbw@public.gmane.org> wrote:> I''m wondering how you would go about putting a "confirm password" field > into a form and not have it inserted into the database. An example > would be on the Basecamp sign up form. And also on that page is an > "accept terms" checkbox. I guess I want to know how do you run these > fields through validation (model or controller) and not have them > automatically entered into the database. > > Russ > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Tobi http://blog.leetsoft.com
I knew it had to be something simple like that. :-) Thanks On Nov 11, 2004, at 12:40 PM, Tobias Luetke wrote:> I added extra fields called password_confirmation and > email_confirmation to my signup form and added this to the controller > : > > @newuser.errors.add("password", "does not match") unless > @newuser.password == @params[''password_confirmation''] > @newuser.errors.add("email", "does not match") unless > @newuser.email == @params[''email_confirmation''] > > > > On Thu, 11 Nov 2004 12:35:34 -0800, Russ Smith <russ-Bff6KPpgmvsxnA8XTFIEbw@public.gmane.org> > wrote: >> I''m wondering how you would go about putting a "confirm password" >> field >> into a form and not have it inserted into the database. An example >> would be on the Basecamp sign up form. And also on that page is an >> "accept terms" checkbox. I guess I want to know how do you run these >> fields through validation (model or controller) and not have them >> automatically entered into the database. >> >> Russ >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > > -- > Tobi > http://blog.leetsoft.com > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Russ Smith wrote:> I''m wondering how you would go about putting a "confirm password" > field into a form and not have it inserted into the database.Simple. Either in the controller confirm_password = @params[''user''].delete(''confirm_password'') or define attr_accessor :confirm_password in the model, it will then be assigned by YourModel.create(@params[''your_form'']), be accessible in validate and other callbacks, and will not be written to the database. Best regards, Alexey Verkhovsky
I would recommend against doing the confirm password validation as part of the model. This would have the disadvantage that you have to supply password confirmation whenever want to create a user. Inconvenient for admin interfaces and especially from IRB. On Thu, 11 Nov 2004 22:55:43 +0200, Alexey Verkhovsky <alex-vV7tgcE2N9Nhl2p70BpVqQ@public.gmane.org> wrote:> Russ Smith wrote: > > > I''m wondering how you would go about putting a "confirm password" > > field into a form and not have it inserted into the database. > > Simple. > > Either in the controller > > confirm_password = @params[''user''].delete(''confirm_password'') > > or define attr_accessor :confirm_password in the model, it will then be > assigned by YourModel.create(@params[''your_form'']), be accessible in > validate and other callbacks, and will not be written to the database. > > Best regards, > Alexey Verkhovsky > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Tobi http://blog.leetsoft.com
If you prefer to keep this behavior in your model, just define an attribute in your Model class. It won''t be persisted since it doesn''t exist in the database, but all the Rails goodies will still play nice. class Model < ActiveRecord::Base attr_accessor :password, :password_confirmation def validate errors.add(''password'', ''Password and confirmation do not match'') unless password_confirmation == password end def before_save self.password_sha1 = Digest::SHA1(password) if password end end Best, jeremy Russ Smith wrote:> I knew it had to be something simple like that. :-) > Thanks > > > On Nov 11, 2004, at 12:40 PM, Tobias Luetke wrote: > >> I added extra fields called password_confirmation and >> email_confirmation to my signup form and added this to the controller >> : >> >> @newuser.errors.add("password", "does not match") unless >> @newuser.password == @params[''password_confirmation''] >> @newuser.errors.add("email", "does not match") unless >> @newuser.email == @params[''email_confirmation''] >> >> >> >> On Thu, 11 Nov 2004 12:35:34 -0800, Russ Smith <russ-Bff6KPpgmvsxnA8XTFIEbw@public.gmane.org> >> wrote: >> >>> I''m wondering how you would go about putting a "confirm password" field >>> into a form and not have it inserted into the database. An example >>> would be on the Basecamp sign up form. And also on that page is an >>> "accept terms" checkbox. I guess I want to know how do you run these >>> fields through validation (model or controller) and not have them >>> automatically entered into the database.
Tobias Luetke wrote:>I would recommend against doing the confirm password validation as >part of the model. > >This would have the disadvantage that you have to supply password >confirmation whenever want to create a user. Inconvenient for admin >interfaces and especially from IRB. > >You can always write the check condition as: if self.confirm_password && self.password == self.confirm_password and if no confirm_password is provided, don''t change the password. The big advantage is consistent use of AR::Errors in the form for reporting mismatched password. Alex
I like the idea of keeping the validation in the model. It just makes the controller code feel cleaner. On Nov 11, 2004, at 1:13 PM, Alexey Verkhovsky wrote:> Tobias Luetke wrote: > >> I would recommend against doing the confirm password validation as >> part of the model. >> >> This would have the disadvantage that you have to supply password >> confirmation whenever want to create a user. Inconvenient for admin >> interfaces and especially from IRB. >> > > You can always write the check condition as: > > if self.confirm_password && self.password == self.confirm_password > > and if no confirm_password is provided, don''t change the password. > > The big advantage is consistent use of AR::Errors in the form for > reporting mismatched password. > > Alex > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
> I like the idea of keeping the validation in the model. It just makes > the controller code feel cleaner.For the record, this is the same approach I use and prefer for Basecamp and other applications. -- David Heinemeier Hansson, http://www.basecamphq.com/ -- Web-based Project Management http://www.rubyonrails.org/ -- Web-application framework for Ruby http://macromates.com/ -- TextMate: Code and markup editor (OS X) http://www.loudthinking.com/ -- Broadcasting Brain