我想验证表单值的格式,允许空值或需要特定格式.
要根据格式验证值,可以这样做:
validates :application_bundle_identifier, :format => {
:with => /^[a-zA-Z0-9.-]+$/,
:message => "A bundle identifier should contain only alphanumeric (A-Z,a-z,0-9), hyphen (-), and period (.) characters."
}
现在,我想允许用户根本不输入值.在本例中,我可以将regexp调整为^[a-zA-Z0-9.-]*$
,但这似乎不是一个很好的Rails方法.根据格式的不同,这可能是不可能的,或者至少不太清楚.
在这种情况下,最佳做法是什么?