ruby on rails - Custom validation after submit button is pressed -
is there way can make link_to submit button run custom validation?
i need writing validation checks presence of fields , make sure there @ least 4 entries in nested form array. im using gem cocoon nested form , project requires user has @ least 4 questions attached application form.
my associations follows:
class app < activerecord::base belongs_to :user has_many :questions, :dependent => :destroy end class question < activerecord::base belongs_to :app end
multistep controller:
class userstepscontroller < applicationcontroller include wicked::wizard steps :personal, :school, :grades, :extra_activity, :paragraph, :submit def show @user = user.find(current_user) case step when :school, :grades, :extra_activity, :paragraph, :submit @question = question.find(current_user) end render_wizard end def update @user = user.find(current_user) @user.update_attributes(user_params) case step when :school, :grades, :extra_activity, :paragraph, :submit @question = question.find(current_user) @question.update_attributes(question_params) end render_wizard @user end
the multistep question form built once user created.
so example on second page of wicked form, i'm trying validate presence of highschool name validates :highschool, :presence => true
im still able submit form on last submit page without highschool filled out.
solution:
i created migration on user called "steps", string.
i add following code user model:
class app < activerecord::base validate :minimum_number_of_questions_required, if: "step == 'extra_activity'" private def minimum_number_of_questions_required if questions.count < 4 errors[:base] << "you must have @ least 4 questions attached." end end end
then in user_steps controller handles turning multiple models single multi page form, added following in update method:
case step when :extra_activity @user.step = step.to_s end
so in simple english, happening rails tracking user based on step of form on. based on step, can run validations specific forms step.
this useful fact need min of 4 questions on extra_activity step, not letting me past previous steps because validation wouldn't let it. remember working same model.
gems used: cocoon (for simple nested forms) , wicked (for simple multistep forms)
if these validations should enforce, , mean whenever attempt save app must have @ least 4 questions add so:
class app < activerecord::base validate :minimum_number_of_questions_required private def minimum_number_of_questions_required if questions.count < 4 errors[:base] << "you must have @ least 4 questions attached." end end end
if wanted occur when form submitted set variable in app class lets object know should run minimum question validation:
class app < activerecord::base attr_accessor :validate_questions validate :minimum_number_of_questions_required, if: :validate_questions # same code above validation end
you have make sure code/form sets validates_question variable value evaluate true. i'm not huge fan of method quick , easy. other think have create sort of facade object or form object sits in front of activerecord model , validations form requires.
class appform include activemodel::validation # move validations code here end
Comments
Post a Comment