对多页表单的复杂模型进行验证

时间:2011-03-28 04:57:40

标签: ruby-on-rails-3 validation nested-forms multipage

我正在尝试使用设计和活跃商家编写注册。表单很复杂,因为我的用户对象如下所示:

class User < ActiveRecord::Base
  include ActiveMerchant::Utils

  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, 
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable

  # Setup accessible (or protected) attributes
  attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :first_name, 
                  :subscription_attributes, :last_name, :zipcode, 
                  :payment_profile_attributes, :customer_cim_id, :payment_profile_id

...

  # Track multi-page registration
  attr_writer :current_step

...

  # Setup Payment Profile element (authorize.net billing profile)
  has_one :payment_profile, :dependent => :delete
  accepts_nested_attributes_for :payment_profile

现在,PaymentProfile类有自己的子项,来自活跃商家的两项:

require 'active_merchant'

class PaymentProfile < ActiveRecord::Base
  include ActiveMerchant::Billing
  include ActiveMerchant::Utils

  validate_on_create :validate_card, :validate_address

  attr_accessor :credit_card, :address

  belongs_to :user

  validates_presence_of :address, :credit_card

  def validate_address
    unless address.valid?
      address.errors.each do |error|
        errors.add( :base, error )
      end
    end
  end

  def address
    @address ||= ActiveMerchant::Billing::Address.new(
      :name     => last_name,
      :address1 => address1,
      :city     => city,
      :state    => state,
      :zip      => zipcode,
      :country  => country,
      :phone    => phone
    )
  end

  def validate_card
    unless credit_card.valid?
      credit_card.errors.full_messages.each do |message|
        errors.add( :base, message )
      end
    end
  end

  def credit_card
    @credit_card ||= ActiveMerchant::Billing::CreditCard.new(
      :type               => card_type,
      :number             => card_number,
      :verification_value => verification_code,
      :first_name         => first_name,
      :last_name          => last_name
    )
    @credit_card.month ||= card_expire_on.month unless card_expire_on.nil?
    @credit_card.year  ||= card_expire_on.year unless card_expire_on.nil?
    return @credit_card
  end

现在我已经覆盖了Devise的RegistrationsController,使用Ryan Bates多页表单截屏(http://railscasts.com/episodes/217-multistep-forms)的解决方案来处理多页表单。我不得不调整它以使其与Devise合作,但我成功了。现在因为Ryan的多页表单只是在不同的页面上询问了同一模型的不同字段,他能够通过在他的验证方法中添加:if块来覆盖他的valid?方法la:

validates_presence_of :username, :if => lambda { |o| o.current_step == "account" }

但在我的情况下,我要求从我的父模型(用户)获取第一个表单上的所有字段,然后询问我的两个孙子模型中的所有字段(用户:PaymentProfile:地址,用户: PaymentProfile:Credit_Card)在第二页。

我面临的问题是虽然PaymentProfile.valid?根据ActiveMerchant的逻辑返回错误,表单本身不呈现甚至显示这些错误。结算页面的查看代码如下所示:

<h2>Payment Details</h2>

<%= semantic_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
    <%= devise_error_messages! %>

    <%= f.semantic_fields_for :payment_profile do |p| %>
        <%= p.semantic_fields_for :address do |a| %>
            <%= a.inputs "Billing Information", :id => "billing" do %>
                <%= a.input :type,    :label => "Credit Card", :as => :select, :collection => get_creditcards %>
                <%= a.input :number,     :label => "Card Number", :as => :numeric %>
                <%= a.input :card_expire_on, :as => :date, :discard_day => true, :start_year => Date.today.year, :end_year => (Date.today.year+10), :add_month_numbers => true %>
                <%= a.input :first_name %>      
                <%= a.input :last_name %>
                <%= a.input :verification_code, :label => "CVV Code" %>
            <% end %>
        <% end %>

        <%= f.semantic_fields_for :credit_card do |c| %>
            <%= c.inputs "Billing Address", :id => "address" do %>
                <%= c.input :address1, :label => "Address" %>
                <%= c.input :city %>
                <%= c.input :state,   :as => :select, :collection => Carmen::states %>
                <%= c.input :country, :as => :select, :collection => Carmen::countries, :selected => 'US' %>
                <%= c.input :zipcode, :label => "Postal Code" %>
                <%= c.input :phone,   :as => :phone %>
            <% end %>
        <% end %>
    <% end %>

    <%= f.commit_button :label => "Continue" %>
    <% unless @user.first_step? %>
    <%= f.commit_button :label => "Back", :button_html => { :name => "back_button" } %>
    <% end %>
<% end %>

我在有效的代码后面的代码中添加了puts errors消息?命令,它显示如下:

{:base=>[["first_name", ["cannot be empty"]], ["last_name", ["cannot be empty"]], ["year", ["expired", "is not a valid year"]], ["type", ["is required", "is invalid"]], ["number", ["is not a valid credit card number"]], ["verification_value", ["is required"]], ["address1", ["is required"]], ["city", ["is required"]], ["state", ["is required"]], ["zip", ["is required", "must be a five digit number"]], ["phone", ["is required", "must be in the format of 333-333-3333"]]]}
{:base=>[["first_name", ["cannot be empty"]], ["last_name", ["cannot be empty"]], ["year", ["expired", "is not a valid year"]], ["type", ["is required", "is invalid"]], ["number", ["is not a valid credit card number"]], ["verification_value", ["is required"]], ["address1", ["is required"]], ["city", ["is required"]], ["state", ["is required"]], ["zip", ["is required", "must be a five digit number"]], ["phone", ["is required", "must be in the format of 333-333-3333"]]]}

现在,此输出的结构与标准错误输出的输出不匹配,标准错误输出是由单层哈希构建的,例如:

{:username=>["can't be blank"]}

所以在向您展示了所有这些之后,我的问题是: a)如何正确显示错误输出,以便表单实际吐出它们? b)如何防止parent.valid?从验证孙子。有效?当我不在那个页面上?我不能使用:if =&gt; lambda ...解决儿童模型,因为他们不知道current_step是什么。

我为这么长的帖子道歉,我只想尽可能多地提供信息。我已经和我一起挣扎了一个星期了,我似乎无法超越它。任何建议都会非常有帮助。提前谢谢。

3 个答案:

答案 0 :(得分:4)

错误未显示的原因可能是它们填充在基础上,而不是单个属性。这种情况会发生在validate_cardvalidate_address方法中。您应该将它们添加到导致错误的特定属性中,而不是将错误添加到base。

errors.add( attr , error )

其次,如果你想让你的验证依赖于某个状态,就像你提到的截屏视频一样,那么你需要用模型保存状态标志(可能最好是父模型)。您可以手动执行此操作,或者更好的是,您可以使用宝石(推荐):state_machine

祝你好运。

答案 1 :(得分:1)

在较高的层面上,您似乎在对象建模中使用继承,并且此模型以几种形式构建,几乎像“向导”一样。我的建议是模拟您的对象以反映实际形式,如

First part of the form collect basic User information : UserInformation model 

Second Part of the form collect payment related information: PaymentInformation model (the Active merchant stuff)

依旧......

如果User模型有一个UserInformation,则有一个PaymentInformation等等。

基本上用Composition替换继承。试着看看你是否可以避免扩展ActiveMerchant框架。

以上样式,可以让您更好地控制何时调用#valid?在您的数据模型的子集上。当用户在表单中移动时,它会逐个构建。

很抱歉,我没有针对您的具体解决方案,而是一种更通用的重写方法。

答案 2 :(得分:0)

我是Ruby-on-Rails的新手,我知道这不会回答上述问题,但您应该尝试Client-Side Validations并查看Rails-casts。它可能对你有所帮助!