ruby - Rails - how to validate 2-steps form? -


i have registration consist of 2 steps. in first one, new user set name, email , password. clicks on "sign up" button , redirected on page second part of registration form (approx 5-7 fields).

i have set validation rules on inputs (7-10 fields). problem is, when fill out first part of form , click on sign button, see validation errors, because fields second part of form not valid.

how avoid behavior?

thank you

you can define virtual attribute used determine attributes validate @ step, use attribute's value on with_options validate required fields @ each step.

something following:

in model:

class mymodal < activerecord::base   attr_accessor :validate_step    with_options if: :validate_step_one? |o|     o.validates :name, presence: true      o.validates :email, presence: true      o.validates :password, presence: true    end    with_options if: :validate_step_two? |o|     ...   end    private:    def validate_step_one?     self.validate_step == 'validate_first_step'   end    def validate_step_two?     self.validate_step == 'validate_second_step'   end end 

then in controller:

class myregistrationcontroller < applicationcontroller   def show     case step       when :first_step         user.validate_step = 'validate_first_step'       when :second_step         user.validate_step = 'validate_second_step'     end   end end 

in controller, in action build object need assign either validate_first_step or validate_second_step based on current step in wizard.

note values , names i've used not descriptive/meaningful, , you'd know better how name them :)


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -