我有一个用改革构建的表单对象,我想使用来自表单的两个值填充一个属性,它们不包含在模型中。
我的表格:
div.form-item.form-item--near
= f.input :employment_contract_type, collection: @presenter.employment_contract_codes_collection
= f.input :employment_contract_time, collection: ["Partial", "Complete"], as: :radio_buttons
我的表单对象:
class FinancingRequestForm < Reform::Form
property :employment_contract_code
property :employment_contract_time, virtual: true
property :employment_contract_type, virtual: true
end
该模型:
# Table name: purchase_financing_customers
#
# id :integer not null, primary key
# employment_contract_code :string
module Purchase
class FinancingCustomer < ApplicationRecord
end
end
我想要这样的东西:
class FinancingRequestForm < Reform::Form
ContractCodePopulator = lambda do |_|
time = employment_contract_type == "Complete" ? "C" : "P"
self.employment_contract_code = "#{send("employment_contract_type")}#{time}"
end
property :employment_contract_code, populator: ContractCodePopulator
property :employment_contract_time, virtual: true
property :employment_contract_type, virtual: true
end
但问题是当我运行 form.validate(request_params) 时 de Populator 不会触发。
我只想要由其他两个虚拟值组成的就业合同代码。
谢谢!