多步骤ActiveRecord的模型验证 [英] Multi-step ActiveRecord's Model validation

查看:65
本文介绍了多步骤ActiveRecord的模型验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑具有以下字段的用户模型:




  • 名字(必填)

  • 姓氏(必填)

  • 电子邮件(必填)

  • 密码(必填)

  • 电话(必填大小:10位数字)

  • 地址(必填)



以及多步骤注册表单并执行以下步骤:




  • 第一步,其中包含名字,姓氏和电子邮件字段

  • 第二步使用密码,电话和地址。



您将如何创建一个解决方案来验证每一步中的输入? / strong>



标准ActiveRecord的方法不起作用,因为它可以一次验证所有字段。



我已经为这个问题创建了一个解决方案,但结果却是一个复杂的代码,所以我正在寻找替代方案。

解决方案方案

我个人应该让模型管理自己的验证并为人类移动逻辑



现在,这可能不是执行此操作的最优雅的方法,而是有效的方法,不会使代码过于混乱。 / p>

向模型添加步骤瞬态属性。

  attr_accessor:step 

将以下方法添加到模型中检查是否在正确的步骤上:

  def on_step(step_number)
step == step_number或step.nil?
end

step.nil的原因?-这样做的好处是,如果您想在此模型上使用验证而不使用步骤,只需不为模型上的step分配值,该方法将允许返回true,从而始终执行验证。



将验证更改为仅在正确的步骤上进行处理,或者在不使用步骤的情况下绕过

  validate:first_name,如果: on_step 1,状态:true 
验证:last_name,如果: on_step 1,状态:true
验证:email,如果: on_step 1 ,状态:true

验证:password,如果: on_step 2,状态:true
验证:phone,如果: on_step 2,格式:{with:TEL_REGEX}, allow_blank:否
验证:address,如果: on_step 2,状态:true

当然,不要忘记为模型设置当前步骤,例如,通过将其硬编码到表单的隐藏字段中(如果渲染)分别为每个步骤创建单独的表单)并更改您的参数以接收它。


Consider a User model with the following fields:

  • First name (required)
  • Last name (required)
  • Email (required)
  • Password (required)
  • Phone (required, size: 10 digits)
  • Address (required)

And a multi-step signup form with the following steps:

  • 1st step with fields First Name, Last Name, and Email
  • 2nd step with Password, Phone and Address.

How would you create a solution to validate the input in each step?

The standard ActiveRecord's way doesn't works, since it validates all fields at once.

I've created a solution for this problem but it turned out in a complicated code, so I'm looking for alternatives.

解决方案

Personally I would let the model manage it's own validation and move the logic for managing validations for each step to that.

Now, this is probably not the most elegant way to do it but its effective and doesn't clutter the code too much.

Add a step transient attribute to the model.

attr_accessor :step

Add the following method to the model to check if on right step:

def on_step(step_number)
  step == step_number or step.nil?
end

The reason for step.nil? - this has the advantage that if you want to use validation on this model without using steps simply don't assign a value for step on your model and the method will allows return true enabling the validation to always be carried out.

Change validations to process only if on right step or to bypass if not using steps

validates :first_name, if: "on_step 1", presence: true
validates :last_name,  if: "on_step 1", presence:true
validates :email,      if: "on_step 1", presence:true

validates :password,   if: "on_step 2", presence:true
validates :phone,      if: "on_step 2", format:{ with: TEL_REGEX  }, allow_blank: false
validates :address,    if: "on_step 2", presence:true

Of course don't forget to set the current step for the model, for example by hard-coding it in a hidden field of the form (if rendering separate forms for each step) and change your params to receive it.

这篇关于多步骤ActiveRecord的模型验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆