How to check for presence of particular value entered?

Hi,

I am entering value in textbox (“name”) on form. Now name field in
database is having ‘unique’ constraint on it so that no 2 records with
same name allowed in database. Now when I m trying to enter same name
into database from my form I m getting following error.

Mysql::Error: #23000Duplicate entry ‘Prashant’ for key 2: INSERT INTO
users (name, password) VALUES(‘Prashant’, ‘Twiari’)

Instead of this error I want the Proper message should be displayed on
same page (“Form page”) with name textbox highlighted. How to do that?
Also How to check presence of particular field value against the value I
ve entered in form so that I can authenticate user for logging in?

Please tell me .

Thanx in advance.
Prash

On Fri, 2006-03-17 at 07:34 +0100, Prashant T. wrote:

Instead of this error I want the Proper message should be displayed on
same page (“Form page”) with name textbox highlighted. How to do that?
Also How to check presence of particular field value against the value I
ve entered in form so that I can authenticate user for logging in?

Please tell me .


you need to do a little background work…

start here…
http://wiki.rubyonrails.org/rails/pages/UnderstandingValidation

the Agile Web D. With Rails is an excellent source of all this
information - you might want to buy the book in paper or PDF form from
your bookseller or from here…

http://www.pragmaticprogrammer.com/bookshelf/index.html

Craig

You want to use
validates_uniqueness_of :name
in your model definition.

http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M000685

And it will be checked upon save of the record.

jt

And as far as showing a nice error message goes, you might want to
check out ‘flash’ in the API…

Regards

Dave M.