Pallavi,
I had faced similar problem, and on observing the sequence of validations
invoked... before_validation* callbacks are called before validate_format...
and is the reason for failure of validation.
I tried in a different way, instead of using validates_format... created a
private method, and performed the regular expression validation in it and on
failure added it to the errors.
call this private method from before_validation_on_create callback.
Something like below
before_validation_on_create :change_format
private
def change_format()
if :credit_card =~ /RE/ then
errors.add('Message')
else
:self.credit_card = credit_card.gsub...
end
end
see if this approach will help you.
Thanks!
Krishna.
On Wed, Jul 22, 2009 at 12:02 AM, Pallavi Naik <[email protected]> wrote:
> Thanks Miga and Joe. I used before_validation instead of before_save and
> before_update and now it doesn't allow me to save duplicate credit card
> numbers. But now i'm facing another problem. Since the before_validation
> gets called before checking the format i'm able to save credit card numbers
> in invalid format such as "1234*A*2345*B*3456*C*4561" or "1234*??*2345*//*
> 3456*--*4567" or "" since the non numeric values get replaced by
> before_validation call as shown below:
>
> # Strip everything but numbers
> def before_validation
> self.credit_card = credit_card.gsub(/[^\d]/, "") if
> attribute_present?("credit_card")
> end
>
> Saving unique credit card numbers and checking their formats seems to be a
> mutually exclusive case. Is there any solution to this? Or is this a case
> which can be handled through proper frontend only?
>
> Cheers,
> Pallavi
>
> Cheers,
> Pallavi
>
>
> On Tue, Jul 21, 2009 at 4:51 AM, Joe Van Overberghe <[email protected]>wrote:
>
>>
>>
>> On Tue, Jul 21, 2009 at 3:27 AM, miga <[email protected]> wrote:
>>
>>> > And if you use
>>> > before_validation_on_create, and before_validation_on_update, it
>>> > should work.
>>>
>> Should work, but is actually a poor programming practice. You should only
>> use the _on_create or _on_update modifiers when you need to do different
>> things on those two events. In this case we always need to normalize the
>> credit card number, so the correct thing to do is to use before_validation
>> with no 'on' modifier. Using the modifier as you suggest, leaves two
>> identical routines inn the code to support and maintain. THis opens the door
>> to the very real possibility for the introduction of subtle bugs if only one
>> of the routines were changed in the future (if this was real world code).
>>
>>>
>>>
>>>
>>
>>
>>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "ruby-on-rails-programming-with-passion" group.
To unsubscribe from this group, send email to
ruby-on-rails-programming-with-passion-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/ruby-on-rails-programming-with-passion?hl=en?hl=en
-~----------~----~----~----~------~----~------~--~---