From c84f3e19b54cf54f525f4b2d158696ae32d1bf60 Mon Sep 17 00:00:00 2001 From: wei <> Date: Tue, 25 Apr 2006 01:00:08 +0000 Subject: Add TListControlValidator. Update client-side validators, datepicker.js, colorpicker.js. --- framework/Web/UI/WebControls/TListControl.php | 52 +++++++++++++++++++++------ 1 file changed, 41 insertions(+), 11 deletions(-) (limited to 'framework/Web/UI/WebControls/TListControl.php') diff --git a/framework/Web/UI/WebControls/TListControl.php b/framework/Web/UI/WebControls/TListControl.php index 37c232e6..1c615edc 100644 --- a/framework/Web/UI/WebControls/TListControl.php +++ b/framework/Web/UI/WebControls/TListControl.php @@ -11,18 +11,13 @@ */ /** - * Includes TDataBoundControl class + * Includes the supporting classes */ Prado::using('System.Web.UI.WebControls.TDataBoundControl'); -/** - * Includes TAttributeCollection class - */ Prado::using('System.Collections.TAttributeCollection'); -/** - * Includes TDataFieldAccessor class - */ Prado::using('System.Util.TDataFieldAccessor'); + /** * TListControl class * @@ -74,8 +69,8 @@ Prado::using('System.Util.TDataFieldAccessor'); * to 'name' and 'age' will make the first item's text be 'John', value be 31, * the second item's text be 'Cary', value be 28, and so on. * The {@link setDataTextFormatString DataTextFormatString} property may be further - * used to format how the item should be displayed. The formatting function is - * the sprintf() PHP function. + * used to format how the item should be displayed. See {@link formatDataValue()} + * for an explanation of the format string. * * @author Qiang Xue * @version $Revision: $ $Date: $ @@ -192,7 +187,7 @@ abstract class TListControl extends TDataBoundControl $text=$object; $item->setValue("$key"); } - $item->setText($textFormat===''?$text:sprintf($textFormat,$text)); + $item->setText($this->formatDataValue($textFormat,$text)); $items->add($item); } // SelectedValue or SelectedIndex may be set before databinding @@ -329,9 +324,10 @@ abstract class TListControl extends TDataBoundControl /** * Sets data text format string. - * The format string is used in sprintf() to format the Text property value + * The format string is used in {@link TDataValueFormatter::format()} to format the Text property value * of each item in the list control. * @param string the formatting string used to control how data bound to the list control is displayed. + * @see TDataValueFormatter::format() */ public function setDataTextFormatString($value) { @@ -585,6 +581,40 @@ abstract class TListControl extends TDataBoundControl } } } + + /** + * Formats the text value according to a format string. + * If the format string is empty, the original value is converted into + * a string and returned. + * If the format string starts with '#', the string is treated as a PHP expression + * within which the token '{0}' is translated with the data value to be formated. + * Otherwise, the format string and the data value are passed + * as the first and second parameters in {@link sprintf}. + * @param string format string + * @param mixed the data to be formatted + * @return string the formatted result + */ + protected function formatDataValue($formatString,$value) + { + if($formatString==='') + return TPropertyValue::ensureString($value); + else if($formatString[0]==='#') + { + $expression=strtr(substr($formatString,1),array('{0}'=>'$value')); + try + { + if(eval("\$result=$expression;")===false) + throw new Exception(''); + return $result; + } + catch(Exception $e) + { + throw new TInvalidDataValueException('listcontrol_expression_invalid',get_class($this),$expression,$e->getMessage()); + } + } + else + return sprintf($formatString,$value); + } } /** -- cgit v1.2.3