0

I have an User model. It has next fields:

attr_accessible :user_name, :first_name, :last_name, :email ....

There is a profile view for the User with 6 blocks. Each of them associated with the various fields. Box 1 - first_name and last_name, Box 2 - user_name and email, etc.

I need to validate all the fields (presence, format, etc). But validators must trigger only for those fields, that has came from a particular block (Box 1 or Box 2, for example).

If I write something like next:

validates :user_name, :presence => true

and I will not edit the block with the *user_name*, I will see the error "user Name can't be blank". I can't use *:allow_blank => true* or nil because it can't(!) be blank!

In two words: I must validate only those fields, that was past from the resquest.

What I can do to solve my problem? Thx

4

2 回答 2

0

您可以添加ifunless选择跳过特定条件。

validates :user_name, :presence => true, :if => "first_name.blank? and last_name.blank?"
于 2012-12-22T16:04:10.197 回答
0

您可以从模型中提取特定字段并为每个块创建一个模型,然后将 one_to_one 关系添加到您的用户模型。

于 2012-12-22T16:30:34.400 回答