Rails客户端验证条件

时间:2014-03-20 12:03:58

标签: ruby-on-rails validation ruby-on-rails-4 mongoid client-side-validation

嗯,有两个div,正如您从代码中看到的那样,一旦页面加载和select_tag值发生变化,我就会动态显示它们。从模型中可以看出,两个div的帮助程序都是按条件在服务器端验证的。我想在客户端做同样的事情。因此尝试仅针对状态强制验证,尝试使用f.object,启用和禁用验证。没运气。 一旦表单被加载,服务器端的条件被加载,所以第一个div通过加载"个人"但是当我加载另一个div(由js);因为条件是为了个人而加载的#34;商业类型,条件为"公司"没有工作,客户端验证不会被解雇。部分使它变得复杂,我无法重新加载页面,因为有没有缓存选择的图片和实时地图数据。

我是否有任何关于客户端验证的信息,您可能会建议我对此进行排序?

模型

 validates :tax_number, length: { is: 10}, presence: true, uniqueness: true,  :if => :company? 
  validates :tax_office, presence: true, :if => :company? 
  validates :tax_business_name, presence: true, :if => :company? 
  validates :tax_pin, length: { is: 11 }, presence: true, uniqueness: true, :if => :personal? 
  validates :tax_fullname, presence: true, :if => :personal? 

  def personal?
    if business_type == 1 || business_type == 0
        return true
    else 
        return false
    end
  end

  def company? 
    if business_type == 2 || business_type == 3
        return true
    else 
        return false
    end
  end

查看

%fieldset
  = f.select :business_type, options_for_select(business_type_options,@provider.business_type),{},:style => "width:150px"
  #taxpersonal
    = f.text_field :tax_fullname, :validate => { :presence => true }, :class=>"input-xlarge"
    = f.text_field :tax_pin, :validate => { :presence => true }, :class=>"input-medium {mask:'99999999999'}", :alt=> "99999999999" 
    .control-group{id:"tax_helper"}
      .controls
        %p.help-block{style:"margin-bottom: 0px; margin-top: -10px;"} 
          %small
            = t('tax_info')
  #taxbusiness
    = f.text_field :tax_business_name, :validate => { :presence => f.object.business_type != 0 ? true : false }, :class=>"input-xxlarge"
    = f.text_field :tax_office,:validate => { :presence => true }, :class=>"input-large"
    = f.text_field :tax_number, :validate => { :presence => true }, :class=>"input-medium {mask:'9999999999'}", :alt=> "9999999999" 
    .control-group
      .controls
        %p.help-block{style:"margin-bottom: 0px; margin-top: -10px;"} 
          %small
            = t('tax_info')

咖啡

  $("#provider_business_type").bind "change", -> 
    if $(this).val() is "0" or $(this).val() is "1"
      $("#taxpersonal").show()
      $("#taxbusiness").hide()
      $('#provider_tax_number').disableClientSideValidations()
      $('#provider_tax_business_name').disableClientSideValidations()
      $('#provider_tax_business_office').disableClientSideValidations()
      $('#provider_tax_pin').enableClientSideValidations()
      $('#provider_tax_fullname').enableClientSideValidations()
    else 
      $("#taxpersonal").hide()
      $("#taxbusiness").show()
      $('#provider_tax_number').enableClientSideValidations()
      $('#provider_tax_business_name').enableClientSideValidations()
      $('#provider_tax_business_office').enableClientSideValidations()
      $('#provider_tax_pin').disableClientSideValidations()
      $('#provider_tax_fullname').disableClientSideValidations()
  $('#provider_business_type').trigger "change"

enter image description here

enter image description here

0 个答案:

没有答案
相关问题