formfields.com Forum Index formfields.com
FORMfields Forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

changing CC expiry date to mm/yy format

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    formfields.com Forum Index -> FF 2.X - Frequently Asked Questions (FAQs)
View previous topic :: View next topic  
Author Message
simonhelps



Joined: 24 May 2006
Posts: 5

PostPosted: Mon Oct 30, 2006 12:15 pm    Post subject: changing CC expiry date to mm/yy format Reply with quote

I am trying to change this field to match how actual ccard expiry dates are shown on cards ( in Australia at least)
I have changed the months "Jan" "Feb" etc in Formfields.php to "01 "02" etc. But the year bit is a bit harder.
I can see the year array being set up using the current year and adding 8 but I cannot work out how to change this to a year format of "06" "07" etc.

Thanks
Back to top
View user's profile Send private message
formfields



Joined: 01 Aug 2005
Posts: 465

PostPosted: Thu Nov 02, 2006 6:21 pm    Post subject: Reply with quote

Here's some code that we've be working on for the next release of the FORMfields Lib that will help you. Just edit your FORMfields.php file and replace the MonthField, YearField and CreditCardDateField with the following code:
Code:

class YearField extends DropDownField {
   var $ENUM_YEARS = null;
   var $ENUM_YEAR_NAMES = null;

   /**
   * @param $dateFormat the format of the outputted date as defined by PHP's date() function. Formats currently supported: y, Y.
   */
   function __construct($name, $label, $required, $blankString, $dateFormat="Y", $yearStart=1999, $yearEnd=2009) {
      $this->ENUM_YEARS = genArray($yearStart, $yearEnd);
      $this->ENUM_NAMES = $this->ENUM_YEARS;
      if ($dateFormat === "y") {
         foreach($this->ENUM_NAMES AS $i=>$year) {
            $this->ENUM_NAMES[$i] = substr($year, 2, 2);
         }
      }
      parent::__construct($name, $label, $required, $this->ENUM_NAMES, $this->ENUM_YEARS, $blankString);
   }

   // Deprecated on 2005.12.19
   function YearField($name, $label, $required, $blankString, $dateFormat="Y") {
      $this->__construct($name, $label, $required, $blankString, $dateFormat);
   }
}

class MonthField extends DropDownField {
   var $ENUM_MONTHS = null;
   var $ENUM_MONTH_NAMES = null;

   /**
   * @param $dateFormat the format of the outputted date as defined by PHP's date() function. Formats currently supported: m, M.
   */
   function __construct($name, $label, $required, $blankString, $dateFormat="M") {
      $this->ENUM_MONTHS = genArray(1, 12);
      if ($dateFormat === "M") {
         $this->ENUM_MONTH_NAMES = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
      } else {
         $this->ENUM_MONTH_NAMES = $this->ENUM_MONTHS;
      }
      parent::__construct($name, $label, $required, $this->ENUM_MONTH_NAMES, $this->ENUM_MONTHS, $blankString);
   }

   // Deprecated on 2005.12.19
   function MonthField($name, $label, $required, $blankString, $dateFormat="m") {
      $this->__construct($name, $label, $required, $blankString, $dateFormat);
   }
}

class CreditCardDateField extends FormField {
   var $month = null;
   var $year = null;

   /**
   * @param $dateFormat the format of the outputted date as defined by PHP's date() function. Formats currently supported: m/y, m/Y, M/y, M/Y.
   */
   function __construct($name, $label, $required, $blankString="", $dateFormat="M/Y") {
      parent::__construct($name, $label, $required);
      $this->month = new MonthField($name . FF_SEP . "month", $name, REQUIRED, $blankString, $dateFormat[0]);
      $date = getdate();
      $this->year = new YearField($name . FF_SEP . "year", $name, REQUIRED, $blankString, $dateFormat[2], $date["year"], $date["year"] + 8);
   }

   // Deprecated on 2005.12.19
   function CreditCardDateField($name, $label, $required, $blankString="", $dateFormat="M/Y") {
      $this->__construct($name, $label, $required, $blankString, $dateFormat);
   }

   function getValue() {
      if ($this->month->getValue() || $this->year->getValue())
         return $this->year->getValue() . "-" . $this->month->getValue();
      return null;
   }

   function getRawDbValue() {
      if ($this->month->getValue() || $this->year->getValue())
         return $this->year->getValue() . "-" . $this->month->getValue() . "-1";
      return null;
   }

   function isValid() {
      if (!parent::isValid())
         return false;
      if ($this->getValue() == null)
         return true;
      if (!$this->month->isValid() || !$this->year->isValid()) {
         $this->setError("is invalid!");
         return false;
      }
      $date = getdate();
      if ($this->year->getValue() < $date["year"]
         || ($this->year->getValue() == $date["year"]
            && $this->month->getValue() < $date["mon"]) ) {
         $this->setError("has expired!");
         return false;
      }
      return true;
   }

   function getEditableFieldTag() {
      return $this->month->getEditableFieldTag() . $this->formatText(" / ") . $this->year->getEditableFieldTag();
   }

   function getParameter() {
      parent::getParameter();
      $this->month->getParameter();
      $this->year->getParameter();
   }

   function clearValue() {
      $this->month->clearValue();
      $this->year->clearValue();
   }

   // Valid formats: YYYY-MM-DD, YYYY-M-D, MM-DD-YYYY
   // Valid separators: "/", "-", "."
   // Returns length of date
   function setDbValue($value) {
      $parts = preg_split("'/|-|\\.| '", $value);
      if (strlen($parts[0]) == 4) {
         $this->year->setValue($parts[0]);
         $this->month->setValue($parts[1]);
      } else {
         $this->year->setValue($parts[2]);
         $this->month->setValue($parts[0]);
      }
      return strlen($parts[0]) + strlen($parts[1]) + strlen($parts[2]) + 2;
   }

   function setValue($value) {
      $this->setDbValue($value);
   }

   /*
   function setDbValue($value) {
      $this->year->setValue(substr($value, 0, 4));
      $this->month->setValue(substr($value, 5, 2));
   }

   function setValue($value) {
      $this->year->setValue(substr($value, 0, 4));
      $this->month->setValue(substr($value, 5, 2));
   }*/

   function getHiddenFieldTag() {
      return $this->month->getHiddenFieldTag() . $this->year->getHiddenFieldTag();
   }

   function setExtraHtml($extraHtml) {
      parent::setExtraHtml($extraHtml);
      $this->month->setExtraHtml($extraHtml);
      $this->year->setExtraHtml($extraHtml);
   }

   function setEnabled($enabled) {
      parent::setEnabled($enabled);
      $this->month->setEnabled($enabled);
      $this->year->setEnabled($enabled);
   }

}

then when using the CreditCardDateField, just append the date format:
Code:

$formBean->addField(new CreditCardDateField("card_date", "Credit Card Date Field", $required, " ", "m/y"));
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    formfields.com Forum Index -> FF 2.X - Frequently Asked Questions (FAQs) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group