lundi 11 septembre 2017

Pass parameter to Validation - Rails 3

We are currently valid different sections via the following code:

on_boarding_form.rb

  def valid?
    @sections.all? {|section| section.valid?}
  end

This goes to on_boarding_section_form.rb. Where we do some checks

  validate do
    @fields.where(:required => true).where(:hidden => false).each do |req_field|

      if req_field.name.to_s == 'FAVOR'
        binding.remote_pry
      end

      if defined? land1
        if land1 == 'BE' && req_field.name.to_s == 'ORT01'
          # do nothing
        elsif land1 == 'BE' && req_field.name.to_s == 'ENTK2'
          # do nothing
        elsif land1 == 'BE' && req_field.name.to_s == 'PSTLZ'
          #validate postal code
          @errors.add_on_blank(req_field.name.downcase.to_sym)
        elsif land1  != 'BE' && req_field.name.to_s == 'ORT01'
          @errors.add_on_blank(req_field.name.downcase.to_sym)
        elsif land1  != 'BE' && req_field.name.to_s == 'ENTK2'
          @errors.add_on_blank(req_field.name.downcase.to_sym)
        elsif land1  != 'BE' && req_field.name.to_s == 'PSTLZ'
          # do nothing
        else
          @errors.add_on_blank(req_field.name.downcase.to_sym)
        end
      else
        @errors.add_on_blank(req_field.name.downcase.to_sym)
      end
    end
    @fields.where(:hidden => false).each do |field|
      next if field.field_type =~ /g/i
      next if field.field_type =~ /d/i
      next if field.field_type =~ /p/i
      validates_with LengthValidator, :attributes => field.name.downcase.to_sym, :in => 0..field.field_length
    end
  end

In sections emergency contact we need to check if the person is married or not. If the person is married the emergency contact fields should be required.

Now my question is in on_boarding_form.rb is possible to pass a parameter to on_boarding_section_form.rb so I can pass the marital status.

Thank you!

Kind regards, Vincent

Aucun commentaire:

Enregistrer un commentaire