Hi,
I am having a problem validating some fields in my model. When I run
the regular expression through the interpreter (irb) it seems to work
properly, but when I try to save through the view, the regular
expression doesn’t seem to catch errors. That is, through the view, I
can save things that don’t match the regular expression.
Here is the model code with the validation:
class OperatorApRecord < ActiveRecord::Base
belongs_to :operator_record, :foreign_key => “operator_id”
has_one :operator_ap_address, :foreign_key =>
“operator_ap_records_id”, :dependent => :destroy
validates_uniqueness_of(:mac, :on => :create, :message => ‘This AP
is already registered to an Operator’)
validates_length_of :mac, :maximum => 17, :message => “must be at
most 17 characters long”
validates_length_of :mac, :minimum => 12, :message => “must be at
least 12 characters long”
validates_format_of :mac, :with => /^[A-Za-z0-9]{2}([:-]?[A-Za-z0-9]
{2}){5}$/, :message => “should be in the format xx:xx:xx:xx:xx:xx”
validates_format_of :latitude, :with => /^-?([0-9]{1,3}).([0-9]
{1,8})$/, :message => “should be in the format
[-]xxx.xxxxxxxx”, :allow_nil => true
validates_format_of :longitude, :with => /^-{0,1}[0-9]{1,3}.[0-9]
{1,8}$/, :message => “should be in the format
[-]xx.xxxxxxxx”, :allow_nil => true
end
This is the controller code:
def add_operator_ap
require_auth
@user.reload
@operator = OperatorRecord.find(:first, :conditions => ["name
= ?",@user.login])
if @request.post?
# update local copies with the info returned from the form.
@operator_ap_record = OperatorApRecord.new({:operator_id =>
@operator.id}.merge(params[:operator_ap_record]))
@operator_ap_address = OperatorApAddress.new(params[:address])
# Try to save the copies.
if @operator_ap_record.save
# If the Record saved, add its new id into the address and
then save it.
@operator_ap_address.operator_ap_records_id =
@operator_ap_record.id
if @operator_ap_address.save
# If all went well, redirect back to the review page.
flash.now['notice'] = "Your AP info has been saved.
\n"
#redirect_to :action => ‘ap_status_review’
end
end
# If there were validation errors we will be returned back to
the form with the values filled out.
else
@operator_ap_record = OperatorApRecord.new
@operator_ap_address = OperatorApAddress.new
end
end
Does anybody have any idea why the validation, which appears to work
correctly in irb, isn’t working in my application?
Thanks,
Simon