formfields
Joined: 01 Aug 2005 Posts: 465
|
Posted: Thu Nov 02, 2006 6:21 pm Post subject: |
|
|
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"));
|
|
|