# Error while testing validatesformatof

**URL:** <https://www.ruby-forum.com/t/error-while-testing-validatesformatof/172083>\
**Category:** Rails\
**Created:** [July 14, 2009, 11:07am UTC](https://www.ruby-forum.com/t/error-while-testing-validatesformatof/172083 "2009-07-14T11:07:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 14, 2009, 11:07am UTC](https://www.ruby-forum.com/t/error-while-testing-validatesformatof/172083/1 "2009-07-14T11:07:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 14, 2009, 11:14am UTC](https://www.ruby-forum.com/t/error-while-testing-validatesformatof/172083/2 "2009-07-14T11:14:19Z")

</div>

On Tue, Jul 14, 2009 at 2:07 AM, Sijo Kg  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> ```
> @role = Factory(:role) #gives :name=>'role1' description =>
> 
> ```
> 
> 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

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. Thus, you might  
want  
to  
rework your regular expression so that it supports the correct naming  
format.

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 14, 2009, 11:24am UTC](https://www.ruby-forum.com/t/error-while-testing-validatesformatof/172083/3 "2009-07-14T11:24:03Z")

</div>

Hi  
I got an answer to use this

/(?\>\w+\s\*(?=,))+/  
([A regex that does not contain comma - Ruby - Ruby-Forum](http://www.ruby-forum.com/topic/191457#834715))

But this in the unless case dont know how to negate this?

Thanks  
Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 15, 2009, 6:04am UTC](https://www.ruby-forum.com/t/error-while-testing-validatesformatof/172083/4 "2009-07-15T06:04:22Z")

</div>

Conrad T. 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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 15, 2009, 7:36am UTC](https://www.ruby-forum.com/t/error-while-testing-validatesformatof/172083/5 "2009-07-15T07:36:42Z")

</div>

On Tue, Jul 14, 2009 at 9:04 PM, Marnen Laibow-Koser \<  
removed\_email\_address@domain.invalid\> wrote:

> Conrad T. 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

> 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](http://www.marnen.org)  
> removed\_email\_address@domain.invalid
