# How to check for presence of particular value entered?

**URL:** <https://www.ruby-forum.com/t/how-to-check-for-presence-of-particular-value-entered/83049>\
**Category:** Rails\
**Created:** [March 17, 2006, 7:34am UTC](https://www.ruby-forum.com/t/how-to-check-for-presence-of-particular-value-entered/83049 "2006-03-17T07:34:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [March 17, 2006, 7:34am UTC](https://www.ruby-forum.com/t/how-to-check-for-presence-of-particular-value-entered/83049/1 "2006-03-17T07:34:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [March 17, 2006, 7:53am UTC](https://www.ruby-forum.com/t/how-to-check-for-presence-of-particular-value-entered/83049/2 "2006-03-17T07:53:12Z")

</div>

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](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](http://www.pragmaticprogrammer.com/bookshelf/index.html)

Craig

---

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [March 17, 2006, 7:53am UTC](https://www.ruby-forum.com/t/how-to-check-for-presence-of-particular-value-entered/83049/3 "2006-03-17T07:53:13Z")

</div>

You want to use  
validates\_uniqueness\_of :name  
in your model definition.

[http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M000685](http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M000685)

And it will be checked upon save of the record.

jt

---

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [March 17, 2006, 7:59am UTC](https://www.ruby-forum.com/t/how-to-check-for-presence-of-particular-value-entered/83049/4 "2006-03-17T07:59:19Z")

</div>

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

Regards

Dave M.
