# Granular model validation based around controller actions?

**URL:** <https://www.ruby-forum.com/t/granular-model-validation-based-around-controller-actions/57945>\
**Category:** Rails\
**Created:** [April 16, 2006, 11:18pm UTC](https://www.ruby-forum.com/t/granular-model-validation-based-around-controller-actions/57945 "2006-04-16T23:18:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [April 16, 2006, 11:18pm UTC](https://www.ruby-forum.com/t/granular-model-validation-based-around-controller-actions/57945/1 "2006-04-16T23:18:20Z")

</div>

How can I achieve granular control over the validation for different  
controllers? I create users from an administration controller, but I  
don’t want to fill in all their information fields, just have their name  
and email address. When they accept the invitation, I want to implement  
validation on the other fields.

I have looked at using :on =\> :update, but I would have to make the user  
update all their information on one page. Is there any way to associate  
the validation with a controller action?

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [April 17, 2006, 12:10am UTC](https://www.ruby-forum.com/t/granular-model-validation-based-around-controller-actions/57945/2 "2006-04-17T00:10:30Z")

</div>

On Apr 16, 2006, at 10:18 pm, Xavier L. wrote:

> update all their information on one page. Is there any way to  
> associate  
> the validation with a controller action?

Xavier

If I have understood you right, this sounds like a database design  
issue. Could you put all the optional data in a separate  
user\_details table, and set a :has\_one relationship in your users class?

Ashley

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [April 17, 2006, 1:54am UTC](https://www.ruby-forum.com/t/granular-model-validation-based-around-controller-actions/57945/3 "2006-04-17T01:54:29Z")

</div>

You could use callbacks like “after\_find” and “before\_validate” to  
delete / replace the **PLACEHOLDER** values IF the user account is not  
validated.

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [April 17, 2006, 1:50am UTC](https://www.ruby-forum.com/t/granular-model-validation-based-around-controller-actions/57945/4 "2006-04-17T01:50:20Z")

</div>

Ashley M. wrote:

> On Apr 16, 2006, at 10:18 pm, Xavier L. wrote:
> 
> > update all their information on one page. Is there any way to  
> > associate  
> > the validation with a controller action?
> 
> Xavier
> 
> If I have understood you right, this sounds like a database design  
> issue. Could you put all the optional data in a separate  
> user\_details table, and set a :has\_one relationship in your users class?
> 
> Ashley

Or, use one table, but put placeholder values in fields that won’t  
validate when empty. You can then detect these placeholders when the  
user goes to edit their profile and remove them before displaying the  
form for editing. Make sure you use something really strange for the  
placeholder, like **PLACEHOLDER**.

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [April 17, 2006, 8:13am UTC](https://www.ruby-forum.com/t/granular-model-validation-based-around-controller-actions/57945/5 "2006-04-17T08:13:04Z")

</div>

Ashley M. wrote:

> put all the optional data in a separate user\_details  
> table, and set a :has\_one relationship in your users class?

Or use the same table in 2 models, 1 looser and 1 stricter :

```
class User < AR:B
end
class StricterUser < User
  validates_presence_of ..
  validates...
end

```

Alain

---

<div class="post-metadata">

**Author:** ![derdewey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@derdewey](https://www.ruby-forum.com/u/derdewey)\
**Post date:** [April 17, 2006, 5:04am UTC](https://www.ruby-forum.com/t/granular-model-validation-based-around-controller-actions/57945/6 "2006-04-17T05:04:48Z")

</div>

Hi !

2006/4/16, Xavier L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> How can I achieve granular control over the validation for different  
> controllers? I create users from an administration controller, but I  
> don’t want to fill in all their information fields, just have their name  
> and email address. When they accept the invitation, I want to implement  
> validation on the other fields.
> 
> I have looked at using :on =\> :update, but I would have to make the user  
> update all their information on one page. Is there any way to associate  
> the validation with a controller action?

I had the same problem, and here’s how I did it:

class User \< AR:B  
validates\_presence\_of :phone, :if =\> :phone\_required?  
attr\_accessor :phone\_required  
alias\_method :phone\_required?, :phone\_required  
end

In the controller, I simply set phone\_required to true if I want the  
phone to be validated. Of course, you could simplify by having a  
single flag for a multiple fields.

Alternatively, do as Ashley said – split the separate fields in a  
secondary table, and pur the validation on that model instead.

Hope that helps !
