Hi I have model Role as validates_presence_of :name validates_presence_of :description validates_format_of :name, :with => /[,]/ And the test to check format as setup do @role = Factory(:role) #gives :name=>''role1'' description => ''testdescript'' end should "validate format of role name" do assert_valid(@org_role) end But when running test get error test: role should have correct name format. (RoleTest): ActiveRecord::RecordInvalid: Validation failed: Role name is invalid But when I comment validates_format_of above everything work ok I cant figure out where the error is Actually I need role name should not contain any commas Please help Thanks in advance Sijo -- Posted via http://www.ruby-forum.com/.
On Tue, Jul 14, 2009 at 2:07 AM, Sijo Kg <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>wrote:> > Hi > I have model Role as > > validates_presence_of :name > validates_presence_of :description > validates_format_of :name, :with => /[,]/ > > And the test to check format as > setup do > @role = Factory(:role) #gives :name=>''role1'' description => > ''testdescript'' > end > should "validate format of role name" do > assert_valid(@org_role) > end > > But when running test get error > > test: role should have correct name format. (RoleTest): > ActiveRecord::RecordInvalid: Validation failed: Role name is invalid > > > But when I comment validates_format_of above everything work ok I cant > figure out where the error is Actually I need role name should not > contain any commas Please help > > Thanks in advance > SijoSijo, what is the correct format for a name? In any case, your regular expression says that the format for the name should be a comma. Thus, you might want to rework your regular expression so that it supports the correct naming format. Good luck, -Conrad --~--~---------~--~----~------------~-------~--~----~ 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 I got an answer to use this /(?>\w+\s*(?=,))+/ (http://www.ruby-forum.com/topic/191457#834715) But this in the unless case dont know how to negate this? Thanks Sijo -- Posted via http://www.ruby-forum.com/.
Conrad Taylor wrote: [...]> Sijo, what is the correct format for a name? In any case, your regular > expression > says that the format for the name should be a comma.No, it says that the name should *contain* a comma (although your brackets are superfluous).> Thus, you might > want > to > rework your regular expression so that it supports the correct naming > format.Yes indeed. If you are requiring names not to have commas, perhaps you want /^[^,]*$/ or something. However, it may be better from a usability perspective to not have this validation -- instead, just remove the commas from the user''s input. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
On Tue, Jul 14, 2009 at 9:04 PM, Marnen Laibow-Koser < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Conrad Taylor wrote: > [...] > > Sijo, what is the correct format for a name? In any case, your regular > > expression > > says that the format for the name should be a comma. > > No, it says that the name should *contain* a comma (although your > brackets are superfluous).validates_format_of :name, :with => /[,]/ is saying that the name should have the pattern, '',''. The definition of ''validates_format_of'' has the following documentation: Validates whether the value of the specified attribute is of the correct form by matching it against the regular expression provided. His initial regular express had the correct syntax and there wasn''t a problem with the brackets. However, he wasn''t using the correct regular expression for the type of names that he wanted to store in his name field. -Conrad> > > Thus, you might > > want > > to > > rework your regular expression so that it supports the correct naming > > format. > > Yes indeed. If you are requiring names not to have commas, perhaps you > want /^[^,]*$/ or something. > > However, it may be better from a usability perspective to not have this > validation -- instead, just remove the commas from the user''s input. > > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org> <marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org> > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---