Archives


Master Symfony2 fundamentals

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).
trainings.sensiolabs.com

Symfony hosting done right

ServerGrove, outstanding support at the right price for your Symfony hosting needs.
servergrove.com

Discover the SensioLabs Support

Access to the SensioLabs Competency Center for an exclusive and tailor-made support on Symfony
sensiolabs.com

Fabien Potencier
New in Symfony 2.2: Payment related validators
by Fabien Potencier – December 12, 2012 – 13 comments

Contributed by
Tim Nagel
in #4734 and #5072.

If you are developing an e-commerce solution based on Symfony2, you might want to validate a credit card number before sending it to your payment gateway. Doing so is very simple in Symfony 2.2 thanks to the Luhn algorithm and its implementation as a validator:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// src/Acme/SubscriptionBundle/Entity/Transaction.php

use Symfony\Component\Validator\Constraints as Assert;

class Transaction
{
    /**
     * @Assert\Luhn(message = "Please check your credit card number.")
     */
    protected $cardNumber;
}

You can also check the card scheme thanks to Symfony\Component\Validator\Constraints\CardSchemeValidator.

Comments RSS

  • Kirill
    #1 Kirill said on the 2012/12/12 at 12:33
    that is great
  • Name
    #2 Name said on the 2012/12/12 at 12:34
    Wow! It's awesome!
  • Max Małecki
    #3 Max Małecki said on the 2012/12/12 at 12:43
    Great job! Financial validators was most buggy place when somebody wrote it in project!
  • Саша Стаменковић
    #4 Саша Стаменковић said on the 2012/12/12 at 12:47
    Cool. I was using https://github.com/umpirsky/zymfony-validator for this.
  • Саша Стаменковић
    #5 Саша Стаменковић said on the 2012/12/12 at 12:49
    BTW, it would be nice to notify translators to provide translation for this new message in as many languages as possible.
  • Elodie IRDOR
    #6 Elodie IRDOR said on the 2012/12/12 at 13:37
    Nice!
  • Francis Hilaire
    #7 Francis Hilaire said on the 2012/12/12 at 14:09
    It's not a Luhn validator, This validator is a number check, php implementation of Luhn algorithm can be found here http://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers#PHP
  • abdelaziz  ben ahmed
    #8 abdelaziz ben ahmed said on the 2012/12/12 at 15:30
    good job
  • Pascal Borreli
    #9 Pascal Borreli said on the 2012/12/12 at 17:51
    @Francis it is the real luhn algorithm, check : https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Validator/Constraints/LuhnValidator.php#L49
  • Darren Black
    #10 Darren Black said on the 2012/12/12 at 22:53
    Wow, I'm loving these 'New in SF2' articles - nice way to communicate the changes without me looking for new features in change logs etc.
  • Chang Long
    #11 Chang Long said on the 2012/12/14 at 02:56
    User may need multi language here.
  • Francis Hilaire
    #12 Francis Hilaire said on the 2012/12/14 at 09:34
    @Pascal that'll teach me to read the articles too fast, thank you for providing the link
  • Michal Klik
    #13 Michal Klik said on the 2012/12/17 at 22:10
    Thumbs up!