Validation problem

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

Hi Simon,

Which validation are you having problems with? All the format_of ?
Just
some of them? And what, exactly are you seeing that’s not what you
expected?

Thanks,
Bill

Hi Bill,

I’m actually having problems with all of the validates_format_of
calls.

An example would be for the latitude validation, the string
“-75.1234asdf” saves successfully through my view, when only numbers
should be allowed.

From the irb prompt, this is what I get:
‘-75.12453627’.match(/^([-]?[0-9]{1,3}).([0-9]{1,8})$/)
=> #MatchData:0x349ef3c

‘-75.1234asdf’.match(/^([-]?[0-9]{1,3}).([0-9]{1,8})$/)
=> nil

So it seems that the regular expression might be okay, but something
else is causing this error.

Thanks,

Simon

Hi Simon,

Simon wrote:

So it seems that the regular expression might be okay,
but something else is causing this error.

I suspect you’re right and that it has something to do with the ordering
of
the validations. It looks to me like you’re using irb rather than
script\console and to test this you’ll need your development
environment.
So, using script\console…

require ‘OperatorApRecord’
testrec = OperatorApRecord.new
testrec.latitude = ‘-75.1234asdf’
testrec.save # should give you some info

I’d recommmend setting up a minimal sandbox app to do your investigation
of
validation ordering. Also, you might find these helpful.

http://www.google.com/search?hl=en&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&hs=PXQ&q=rails+order+of+validation&btnG=Search

http://dev.rubyonrails.org/ticket/6657

Let us know what you find out!

Best regards,
Bill