# File upload validations?

**URL:** <https://www.ruby-forum.com/t/file-upload-validations/67720>\
**Category:** Rails\
**Created:** [November 17, 2005, 10:26am UTC](https://www.ruby-forum.com/t/file-upload-validations/67720 "2005-11-17T10:26:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cool\_screen\_name900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@cool\_screen\_name900](https://www.ruby-forum.com/u/cool_screen_name900)\
**Post date:** [November 17, 2005, 10:26am UTC](https://www.ruby-forum.com/t/file-upload-validations/67720/1 "2005-11-17T10:26:21Z")

</div>

It appears there are no validation class methods that can be used on  
file uploads? These would be  
nice 🙂

validates\_file\_type  
validates\_file\_size

csn

* * *

Yahoo! FareChase: Search multiple travel sites in one click.  
[http://farechase.yahoo.com](http://farechase.yahoo.com)

---

<div class="post-metadata">

**Author:** ![cool\_screen\_name900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@cool\_screen\_name900](https://www.ruby-forum.com/u/cool_screen_name900)\
**Post date:** [November 17, 2005, 10:54am UTC](https://www.ruby-forum.com/t/file-upload-validations/67720/2 "2005-11-17T10:54:46Z")

</div>

cool\_screen\_name90001 wrote:

> It appears there are no validation class methods that can be used on  
> file uploads? These would be  
> nice 🙂
> 
> validates\_file\_type  
> validates\_file\_size
> 
> csn
> 
> * * *
> 
> Yahoo! FareChase: Search multiple travel sites in one click.  
> [http://farechase.yahoo.com](http://farechase.yahoo.com)

Those validations would only make sense on a UploadedFileClass…As this  
class doesnt exsist in rails, there’s no need for these validations in  
the core…

But it would be an excellent code snippet…

Mikkel

---

<div class="post-metadata">

**Author:** ![cool\_screen\_name900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@cool\_screen\_name900](https://www.ruby-forum.com/u/cool_screen_name900)\
**Post date:** [November 18, 2005, 4:26am UTC](https://www.ruby-forum.com/t/file-upload-validations/67720/3 "2005-11-18T04:26:04Z")

</div>

Turns out it’s pretty easy.

In item’s create view:

\<%= file\_field ‘item’, ‘file’ %\>

and item’s model:

class Item \< ActiveRecord::Base  
validate :check\_file\_size

```
def check_file_size
	if @file and @file.size > 20000
		errors.add('file', 'too big')
	end
end

def file
	@file
end

def file=(file)
	@file=file
end

```

end

csn

— Mikkel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> But it would be an excellent code snippet…
> 
> Mikkel
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

* * *

Yahoo! FareChase: Search multiple travel sites in one click.  
[http://farechase.yahoo.com](http://farechase.yahoo.com)
