简体   繁体   English

活动管理员输入两个日期验证

[英]Active Admin input two dates validation

When I'm creating new object model in ActiveAdmin for bonus table I would like to be able to validate if start_date is lower than end_date.当我在 ActiveAdmin 中为bonus表创建新的 object model 时,我希望能够验证 start_date 是否低于 end_date。 I tried using我尝试使用

def create
  if params[:current_period_start] > params[:current_period_end]
    return redirect...
  end
  super 
end

but it fails because my request data looks like this:但它失败了,因为我的请求数据如下所示:

"bonus"=>{"current_period_start(1i)"=>"2018", "current_period_start(2i)"=>"3",
"current_period_start(3i)"=>"2", "current_period_start(4i)"=>"02", 
"current_period_start(5i)"=>"03", "current_period_end(1i)"=>"2022", 
"current_period_end(2i)"=>"3", "current_period_end(3i)"=>"5", 
"current_period_end(4i)"=>"04", "current_period_end(5i)"=>"05"}, "id"=>"14"}

Can someone suggest me how to transform this into whole date?有人可以建议我如何将其转换为整个日期吗? Or another simpler solution或者另一个更简单的解决方案

Do you need to redefine the create method in the ActiveAdmin controller?是否需要重新定义ActiveAdmin controller 中的create方法? I would remove the whole create method and add a custom validation in the Bonus model (which I assume is located at app/models/bonus.rb )我将删除整个create方法并在Bonus model 中添加自定义验证(我假设它位于app/models/bonus.rb

class Bonus < ApplicationRecord
  validates :current_period_start_should_be_before_current_period_end
  def current_period_start_should_be_before_current_period_end
    if current_period_end < current_period_start
      errors.add(:current_period_start, "Should be before current period end")
    end
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM