# Validation problem

**URL:** <https://www.ruby-forum.com/t/validation-problem/89878>\
**Category:** Rails\
**Created:** [March 23, 2007, 2:21pm UTC](https://www.ruby-forum.com/t/validation-problem/89878 "2007-03-23T14:21:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [March 23, 2007, 2:21pm UTC](https://www.ruby-forum.com/t/validation-problem/89878/1 "2007-03-23T14:21:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [March 23, 2007, 3:12pm UTC](https://www.ruby-forum.com/t/validation-problem/89878/2 "2007-03-23T15:12:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [March 23, 2007, 4:02pm UTC](https://www.ruby-forum.com/t/validation-problem/89878/3 "2007-03-23T16:02:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [March 23, 2007, 4:28pm UTC](https://www.ruby-forum.com/t/validation-problem/89878/4 "2007-03-23T16:28:03Z")

</div>

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://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](http://dev.rubyonrails.org/ticket/6657)

Let us know what you find out!

Best regards,  
Bill
