验证非模型字段 [英] Validate non-model field

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

问题描述

我在我的表单中添加了一个额外的字段:

I've added an extra field to my new form:

<%= select_tag :quantity, options_for_select(["Select a Value"].concat((1..10).to_a)) %>

它指定要创建的记录的副本数.

It specifies the number of copies of the record to be created.

我如何验证该字段的存在(或数量),因为它不是模型本身的一部分?

How can I validate the presence (or numericality) of that field, as it's not part of the model itself?

validates_presence_of :quantity 失败!!!

推荐答案

已找到.您可能希望在模型中添加一个虚拟属性.

Found. You may want to add a virtual attribute in the model.

.........

attr_accessor :not_on_db
.........

validates_presence_of     :not_on_db,
validates_length_of       :not_on_db,    :within => 1..5
.........

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

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