Validate method

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

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.

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

On Thu, Aug 7, 2008 at 1:45 AM, Ryan B. [email protected]
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.

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?