# Validate method

**URL:** <https://www.ruby-forum.com/t/validate-method/145146>\
**Category:** Rails\
**Created:** [August 6, 2008, 2:06pm UTC](https://www.ruby-forum.com/t/validate-method/145146 "2008-08-06T14:06:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [August 6, 2008, 2:06pm UTC](https://www.ruby-forum.com/t/validate-method/145146/1 "2008-08-06T14:06:14Z")

</div>

Hi,

I want to validate a string before saving, but I need a custom method  
because I want to change stuff in the string.

I need to remove all whitespaces (front, back, inside) of  
@movie.filename  
How I can do it, @movie is the model to save.

protected  
def validate

# I think I put my code here to modify filename

end

secondly, after I modify my string I want to verify if it contains only  
:  
A-Z  
a-z  
0-9  
dash -  
underscore \_

I’m not familiar with regular expression is it:

errors.add(“filename”, “has invalid format”) unless filename =~  
/[a-zA-Z0-9-\_]\*/

BR  
David

---

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [August 6, 2008, 3:30pm UTC](https://www.ruby-forum.com/t/validate-method/145146/2 "2008-08-06T15:30:52Z")

</div>

Suppose I have a Model called House with a field called name

how can I validate the name field, I tried name in the validate method  
but  
it doesn’t work, i tried :name, House.name, I don’t know how I can  
validate this field within a validate method, I don’t know how I can  
access it.

---

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [August 6, 2008, 4:52pm UTC](https://www.ruby-forum.com/t/validate-method/145146/3 "2008-08-06T16:52:41Z")

</div>

> Suppose I have a Model called House with a field called name  
> how can I validate the name field, I tried name in the validate method

It will be self.name

In reference to your earlier email about string manipulation and  
validation:

You can put your string manipulations in a before\_validate method,  
and then declare another function to validate if it is valid.

If you call that check\_movie\_filename, then you should have a line

validate :check\_movie\_filename

somewhere in your model.

Read up on ActiveRecord callbacks if you want to learn more about  
these mechanisms.

Franz

---

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [August 7, 2008, 9:44am UTC](https://www.ruby-forum.com/t/validate-method/145146/4 "2008-08-07T09:44:00Z")

</div>

On Thu, Aug 7, 2008 at 1:45 AM, Ryan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> that validate gets called on. You don’t call self.self.name so why  
> call self.name?

Putting self makes it clear that I’m using the model’s attribute  
accessors, and not a local variable within my method.

---

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [August 7, 2008, 3:19am UTC](https://www.ruby-forum.com/t/validate-method/145146/5 "2008-08-07T03:19:07Z")

</div>

You could just call name in the validate method (assuming you’re  
redefining it) rather than self.name, as self will return the object  
that validate gets called on. You don’t call self.self.name so why  
call self.name?
