I thought some of you symfony developers out there might appreciate the following form validators. They have been incredibly convenient, and were incredibly simple to write. I hope they come in handy:

  1. sfValidatorPhone: Validates a phone number using regex
  2. sfValidatorZip: Validates a zip code using regex
  3. sfValidatorUrl: Validates a url using regex
  4. sfValidatorCCExpirationDate (1.2, 1.4): validates the month/year on a credit card. Goes well with sfWidgetFormCCExpirationDate.
  5. sfValidatorCreditCardNumber: validates a credit card number with an optional card type parameter.
  6. sfValidatorCoupon: Used in payment forms to validate a coupon

What other useful validators should I include?

***** UPDATE *****

Symfony versions 1.3 and 1.4 broke backwards compatibility with some of these validators. I’ve added a link to each symfony version to fix this.


  • Ethan - October 5, 2009

    Thanks for sharing these! You have saved me some time and research.

  • Stereoscott - November 6, 2009

    Thanks a lot! I was searching for a phone number validator and was happy to find this great collection. FWIW, Symfony 1.3 includes an sfValidatorUrl class.

  • Cryo - January 28, 2010

    Exactly what I was looking for (Zip and Phone). Thank you.

  • Joshua - March 29, 2010

    Hell yeah! I was just thiiiiiis close to re-implementing sfValidatorPhone, but then, in a flash, I remembered, “I think Brent already did this somewhere.”

    And of course, you did. Thanks for putting this up!

  • David - April 1, 2010

    It’s quite possible that I’m not using your sfValidatorCCExpirationDate properly, but it seems that I must change the method signature to ‘convertDateArrayToString’ in order for it to override the method in sfValidatorDate. I’m using Symfony 1.4.1 if that helps.

  • Brent Shaffer - April 29, 2010

    I just found this out myself… took me about a half hour. I have updated my post accordingly. Thanks for the update, David!

  • Phil - June 7, 2011

    Hi – just been looking for a credit card validator for sf1.4 and came across this page.

    I think there is a problem with the regex in sfValidatorCreditCardNumber for switch / jcb /enroute.

    I think the square brackets should be parentheses if you want to match the 4 digits as a group, e.g.


    Anyway, I realise its a bit late now but thought I’d add a note for anyone else in the same boat.

  • nitin - November 5, 2011

    this is very use ful validatiors…. thanx for this :) :)

  • Pedro - February 16, 2012


  • nazli - April 17, 2012

    Amazing !!! Thanks