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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Mar 23, 10:12 am, "Bill Walton" <bill.wal...-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote:> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Mar 23, 10:12 am, "Bill Walton" <bill.wal...-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote:> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 infoI''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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---