# Validating non-model forms - a predicament

**URL:** <https://www.ruby-forum.com/t/validating-non-model-forms-a-predicament/156750>\
**Category:** Rails\
**Created:** [January 2, 2009, 11:21am UTC](https://www.ruby-forum.com/t/validating-non-model-forms-a-predicament/156750 "2009-01-02T11:21:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lynchnco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@lynchnco](https://www.ruby-forum.com/u/lynchnco)\
**Post date:** [January 2, 2009, 11:21am UTC](https://www.ruby-forum.com/t/validating-non-model-forms-a-predicament/156750/1 "2009-01-02T11:21:58Z")

</div>

Hey folks,  
I have encountered what’s beginning to look like a classic problem with  
Rails: validating non-model forms. Under what circumstances do you need  
to use non-model components in a model-backed form? I need to for credit  
card processing: I want to save information like the customer’s phone  
number but I don’t want to save their credit card number for security  
reasons. Apparently, this situation also comes up when setting up basic  
search forms for your website as well  
([#37 Simple Search Form - RailsCasts](http://railscasts.com/episodes/37-simple-search-form)); unfortunately,  
Ryan didn’t address it.

Essentially we’re looking at this situation in the checkout view:

\<% form\_for(@checkout, :url =\> “/checkout”) do |f| %\>  
\<%= f.text\_field :first\_name %\>  
\<%= f.text\_field :last\_name %\>  
\<%= text\_field(:cc, :card) %\>  
\<%= text\_field(:cc, :expiration\_date) %\>  
\<% end %\>

But when the model is saved, the :card and :expiration\_date fields are  
not validated because they’re not part of the model (they are accessed  
using params[:cc][:card] and params[:cc][:expiration\_date]).

All this code works - I just need to validate my credit card stuff! How  
do I do this? I know I’m not the first guy to run into this problem.

Thank you for any suggestions!

---

<div class="post-metadata">

**Author:** ![lynchnco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@lynchnco](https://www.ruby-forum.com/u/lynchnco)\
**Post date:** [January 2, 2009, 12:16pm UTC](https://www.ruby-forum.com/t/validating-non-model-forms-a-predicament/156750/2 "2009-01-02T12:16:29Z")

</div>

Patrick L. wrote:

> But when the model is saved, the :card and :expiration\_date fields are  
> not validated because they’re not part of the model (they are accessed  
> using params[:cc][:card] and params[:cc][:expiration\_date]).  
> Add them as attributes to the model and then create validation rules as  
> normal -

attr\_accessible :expiration\_date

validates\_presence\_of :expiration\_date

---

<div class="post-metadata">

**Author:** ![lynchnco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@lynchnco](https://www.ruby-forum.com/u/lynchnco)\
**Post date:** [January 2, 2009, 3:58pm UTC](https://www.ruby-forum.com/t/validating-non-model-forms-a-predicament/156750/3 "2009-01-02T15:58:15Z")

</div>

Patrick L. wrote:

> Hey folks,  
> I have encountered what’s beginning to look like a classic problem with  
> Rails: validating non-model forms. Under what circumstances do you need  
> to use non-model components in a model-backed form? I need to for credit  
> card processing: I want to save information like the customer’s phone  
> number but I don’t want to save their credit card number for security  
> reasons.

Add attr\_accessor :cc\_number to your model, then validate it.

Then call .save(false) when saving the model object internally, to  
bypass all  
your validations.

Everything will work as if you had a column with that name, except  
find\_by\_cc\_number, or loading the number (natch).

Also f.text\_field will work, so Card.new(params[:card]) will work.

–  
Phlip

---

<div class="post-metadata">

**Author:** ![lynchnco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@lynchnco](https://www.ruby-forum.com/u/lynchnco)\
**Post date:** [January 2, 2009, 9:08pm UTC](https://www.ruby-forum.com/t/validating-non-model-forms-a-predicament/156750/4 "2009-01-02T21:08:18Z")

</div>

Andrew P. wrote:

> Patrick L. wrote:
> 
> > But when the model is saved, the :card and :expiration\_date fields are  
> > not validated because they’re not part of the model (they are accessed  
> > using params[:cc][:card] and params[:cc][:expiration\_date]).  
> > Add them as attributes to the model and then create validation rules as  
> > normal -
> 
> attr\_accessible :expiration\_date
> 
> validates\_presence\_of :expiration\_date

Thank you both. I implemented Andrew’s solution, and it seems to work.  
Thing is, it doesn’t wrap the expiration\_date input field in the error  
div (in other words, it doesn’t turn red).

How should I fix this?

---

<div class="post-metadata">

**Author:** ![lynchnco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@lynchnco](https://www.ruby-forum.com/u/lynchnco)\
**Post date:** [May 6, 2009, 11:01pm UTC](https://www.ruby-forum.com/t/validating-non-model-forms-a-predicament/156750/5 "2009-05-06T23:01:40Z")

</div>

I second this.

I have a User model where a user signs up and enters basic  
information, and then later enters more information to access advanced  
features.

If I put validates\_presence\_of in the model for the fields in the  
second form, Rails won’t let me create the user in the first form  
without those fields.

How can I get around this?

On Jan 2, 3:08 pm, “Patrick L.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
