From 8b0c8f0d0557d8ab88409cd9691cd1ee279321d5 Mon Sep 17 00:00:00 2001 From: wei <> Date: Tue, 25 Apr 2006 07:01:04 +0000 Subject: Complete #70, update TDateFormat, NumberFormat add zerofill. Update TDatePicker quickstart doc, I18N quickstart doc. --- framework/I18N/TDateFormat.php | 11 ++-- framework/I18N/core/DateFormat.php | 12 +++- framework/I18N/core/NumberFormat.php | 7 ++- framework/I18N/core/NumberFormatInfo.php | 18 ++++++ framework/Web/Javascripts/js/validator.js | 50 +++++++++------ framework/Web/Javascripts/prado/validation3.js | 71 +++++++++++++++------- .../Web/UI/WebControls/TDataTypeValidator.php | 13 ++++ 7 files changed, 129 insertions(+), 53 deletions(-) (limited to 'framework') diff --git a/framework/I18N/TDateFormat.php b/framework/I18N/TDateFormat.php index 8ebbce5e..0f5507ab 100644 --- a/framework/I18N/TDateFormat.php +++ b/framework/I18N/TDateFormat.php @@ -48,10 +48,9 @@ Prado::using('System.I18N.TI18NControl'); * if the Value property is not specified. * - Pattern, string, *
Gets or sets the formatting pattern. The predefined patterns are - * 'full date', 'long date', 'medium date', 'short date', 'full time', - * 'long time', 'medium time', and 'short time'. Custom patterns can - * specified when the Pattern property does not match the predefined - * patterns. + * 'fulldate', 'longdate', 'mediumdate', 'shortdate', 'fulltime', + * 'longtime', 'mediumtime', and 'shorttime'. Custom patterns can specified + * when the Pattern property does not match the predefined patterns. * * @author Xiang Wei Zhuo * @version v1.0, last update on Sat Dec 11 15:25:11 EST 2004 @@ -69,7 +68,7 @@ class TDateFormat extends TI18NControl * A set of pattern presets and their respective formatting shorthand. * @var array */ - protected $patternPresets = array( + static private $_patternPresets = array( 'fulldate'=>'P','full'=>'P', 'longdate'=>'D','long'=>'d', 'mediumdate'=>'p','medium'=>'p', @@ -132,7 +131,7 @@ class TDateFormat extends TI18NControl protected function getPreset($string) { $string = strtolower($string); - foreach($this->patternPresets as $pattern => $preset) + foreach(self::$_patternPresets as $pattern => $preset) { if($string == $pattern) return $preset; diff --git a/framework/I18N/core/DateFormat.php b/framework/I18N/core/DateFormat.php index 9caca03b..142926f4 100644 --- a/framework/I18N/core/DateFormat.php +++ b/framework/I18N/core/DateFormat.php @@ -128,7 +128,12 @@ class DateFormat if($pattern{0} == "'" && $pattern{strlen($pattern)-1} == "'") { - $tokens[$i] = preg_replace('/(^\')|(\'$)/','',$pattern); + $sub = preg_replace('/(^\')|(\'$)/','',$pattern); + $tokens[$i] = str_replace('``````','\'',$sub); + } + else if($pattern == '``````') + { + $tokens[$i] = '\''; } else { @@ -145,7 +150,7 @@ class DateFormat throw new Exception('function '.$function.' not found.'); } - } + } } return I18N_toEncoding(implode('',$tokens), $charset); @@ -269,6 +274,7 @@ class DateFormat $token = null; $text = false; + $pattern = preg_replace("/''/", '``````', $pattern); for($i = 0; $i < strlen($pattern); $i++) { @@ -278,7 +284,7 @@ class DateFormat } else { - $tokens[] = str_replace("''","'",$token); + $tokens[] = str_replace("","'",$token); $token = $pattern{$i}; } diff --git a/framework/I18N/core/NumberFormat.php b/framework/I18N/core/NumberFormat.php index bde87e65..d1184f86 100644 --- a/framework/I18N/core/NumberFormat.php +++ b/framework/I18N/core/NumberFormat.php @@ -63,7 +63,7 @@ require_once(dirname(__FILE__).'/util.php'); * $ja = new NumberFormat('ja_JP'); * * //Japanese currency pattern, and using Japanese Yen symbol - * $ja->format(123.14,'c','JPY'); //�123 (Yen 123) + * $ja->format(123.14,'c','JPY'); //�?123 (Yen 123) * * For each culture, the symbol for each currency may be different. * @@ -166,12 +166,17 @@ class NumberFormat $string = substr($string, 0, $dp); $integer = ''; + + $digitSize = $this->formatInfo->getDigitSize(); + + $string = str_pad($string, $digitSize, '0',STR_PAD_LEFT); $len = strlen($string); $groupSeparator = $this->formatInfo->GroupSeparator; $groupSize = $this->formatInfo->GroupSizes; + $firstGroup = true; $multiGroup = is_int($groupSize[1]); $count = 0; diff --git a/framework/I18N/core/NumberFormatInfo.php b/framework/I18N/core/NumberFormatInfo.php index 15b9ee5d..3531cc7f 100644 --- a/framework/I18N/core/NumberFormatInfo.php +++ b/framework/I18N/core/NumberFormatInfo.php @@ -324,6 +324,13 @@ class NumberFormatInfo } } } + + if(is_int($decimalPos)) + $digitPattern = substr($pattern,0,$decimalPos); + else + $digitPattern = $pattern; + + $digitPattern = preg_replace('/[^0]/','',$digitPattern); $info['groupPos1'] = $groupPos1; $info['groupSize1'] = $groupSize1; @@ -331,6 +338,7 @@ class NumberFormatInfo $info['groupSize2'] = $groupSize2; $info['decimalPos'] = $decimalPos; $info['decimalPoints'] = $decimalPoints; + $info['digitSize'] = strlen($digitPattern); return $info; } @@ -365,6 +373,16 @@ class NumberFormatInfo return $this->pattern['decimalPoints'] = $value; } + function getDigitSize() + { + return $this->pattern['digitSize']; + } + + function setDigitSize($value) + { + $this->pattern['digitSize'] = $value; + } + /** * Gets the string to use as the decimal separator. * @return string decimal separator. diff --git a/framework/Web/Javascripts/js/validator.js b/framework/Web/Javascripts/js/validator.js index 38d8a2a4..8659d2e9 100644 --- a/framework/Web/Javascripts/js/validator.js +++ b/framework/Web/Javascripts/js/validator.js @@ -353,7 +353,7 @@ validator.manager.updateSummary(validator.group); this._isObserving[control.id+this.options.ID] = true; } }, -trim : function(value) +_trim : function(value) { return typeof(value) == "string" ? value.trim() : ""; }, @@ -393,20 +393,20 @@ getValidationValue : function(control) { case 'TDatePicker': if(control.type == "text") - return this.trim($F(control)); + return this._trim($F(control)); else { - this.observeDatePickerChanges(); + this._observeDatePickerChanges(); return Prado.WebUI.TDatePicker.getDropDownDate(control).getTime(); } default: - if(this.isListControlType()) - return this.getFirstSelectedListValue(); + if(this._isListControlType()) + return this._getFirstSelectedListValue(); else - return this.trim($F(control)); + return this._trim($F(control)); } }, -observeDatePickerChanges : function() +_observeDatePickerChanges : function() { if(Prado.Browser().ie) { @@ -416,11 +416,11 @@ this.observeChanges(DatePicker.getMonthListControl(this.control)); this.observeChanges(DatePicker.getYearListControl(this.control)); } }, -getSelectedValuesAndChecks : function(elements, initialValue) +_getSelectedValuesAndChecks : function(elements, initialValue) { var checked = 0; var values = []; -var isSelected = this.isCheckBoxType(elements[0]) ? 'checked' : 'selected'; +var isSelected = this._isCheckBoxType(elements[0]) ? 'checked' : 'selected'; elements.each(function(element) { if(element[isSelected] && element.value != initialValue) @@ -431,7 +431,7 @@ values.push(element.value); }); return {'checks' : checked, 'values' : values}; }, -getListElements : function() +_getListElements : function() { switch(this.options.ControlType) { @@ -440,7 +440,7 @@ var elements = []; for(var i = 0; i < this.options.TotalItems; i++) { var element = $(this.options.ControlToValidate+"_"+i); -if(this.isCheckBoxType(element)) +if(this._isCheckBoxType(element)) elements.push(element); } return elements; @@ -457,7 +457,7 @@ default: return []; } }, -isCheckBoxType : function(element) +_isCheckBoxType : function(element) { if(element && element.type) { @@ -466,18 +466,18 @@ return type == "checkbox" || type == "radio"; } return false; }, -isListControlType : function() +_isListControlType : function() { var list = ['TCheckBoxList', 'TRadioButtonList', 'TListBox']; return list.include(this.options.ControlType); }, -getFirstSelectedListValue : function() +_getFirstSelectedListValue : function() { var initial = ""; if(typeof(this.options.InitialValue) != "undefined") initial = this.options.InitialValue; -var elements = this.getListElements(); -var selection = this.getSelectedValuesAndChecks(elements, initial); +var elements = this._getListElements(); +var selection = this._getSelectedValuesAndChecks(elements, initial); return selection.values.length > 0 ? selection.values[0] : initial; } } @@ -493,7 +493,7 @@ return true; else { var a = this.getValidationValue(); -var b = this.trim(this.options.InitialValue); +var b = this._trim(this.options.InitialValue); return(a != b); } } @@ -595,16 +595,16 @@ Prado.WebUI.TListControlValidator = Class.extend(Prado.WebUI.TBaseValidator, { evaluateIsValid : function() { -var elements = this.getListElements(); +var elements = this._getListElements(); if(elements && elements.length <= 0) return true; this.observeListElements(elements); -var selection = this.getSelectedValuesAndChecks(elements); +var selection = this._getSelectedValuesAndChecks(elements); return this.isValidList(selection.checks, selection.values); }, observeListElements : function(elements) { -if(Prado.Browser().ie && this.isCheckBoxType(elements[0])) +if(Prado.Browser().ie && this._isCheckBoxType(elements[0])) { var validator = this; elements.each(function(element) @@ -640,3 +640,13 @@ required = this.options.Required.split(/,\s*/); return required; } }); +Prado.WebUI.TDataTypeValidator = Class.extend(Prado.WebUI.TBaseValidator, +{ +evaluateIsValid : function() +{ +var value = this.getValidationValue(); +if(value.length <= 0) +return true; +return this.convert(this.options.DataType, value) != null; +} +}); diff --git a/framework/Web/Javascripts/prado/validation3.js b/framework/Web/Javascripts/prado/validation3.js index 4c189532..40472e7e 100644 --- a/framework/Web/Javascripts/prado/validation3.js +++ b/framework/Web/Javascripts/prado/validation3.js @@ -667,9 +667,9 @@ Prado.WebUI.TBaseValidator.prototype = }, /** - * @return string trims the string value, empty string if value is not string. + * @return string _trims the string value, empty string if value is not string. */ - trim : function(value) + _trim : function(value) { return typeof(value) == "string" ? value.trim() : ""; }, @@ -720,25 +720,26 @@ Prado.WebUI.TBaseValidator.prototype = { case 'TDatePicker': if(control.type == "text") - return this.trim($F(control)); + return this._trim($F(control)); else { - this.observeDatePickerChanges(); + this._observeDatePickerChanges(); return Prado.WebUI.TDatePicker.getDropDownDate(control).getTime(); } default: - if(this.isListControlType()) - return this.getFirstSelectedListValue(); + if(this._isListControlType()) + return this._getFirstSelectedListValue(); else - return this.trim($F(control)); + return this._trim($F(control)); } }, /** * Observe changes in the drop down list date picker, IE only. + * @private */ - observeDatePickerChanges : function() + _observeDatePickerChanges : function() { if(Prado.Browser().ie) { @@ -753,12 +754,13 @@ Prado.WebUI.TBaseValidator.prototype = * Gets numeber selections and their values. * @return object returns selected values in values property * and number of selections in checks property. + * @private */ - getSelectedValuesAndChecks : function(elements, initialValue) + _getSelectedValuesAndChecks : function(elements, initialValue) { var checked = 0; var values = []; - var isSelected = this.isCheckBoxType(elements[0]) ? 'checked' : 'selected'; + var isSelected = this._isCheckBoxType(elements[0]) ? 'checked' : 'selected'; elements.each(function(element) { if(element[isSelected] && element.value != initialValue) @@ -774,8 +776,9 @@ Prado.WebUI.TBaseValidator.prototype = * Gets an array of the list control item input elements, for TCheckBoxList * checkbox inputs are returned, for TListBox HTML option elements are returned. * @return array list control option elements. + * @private */ - getListElements : function() + _getListElements : function() { switch(this.options.ControlType) { @@ -784,7 +787,7 @@ Prado.WebUI.TBaseValidator.prototype = for(var i = 0; i < this.options.TotalItems; i++) { var element = $(this.options.ControlToValidate+"_"+i); - if(this.isCheckBoxType(element)) + if(this._isCheckBoxType(element)) elements.push(element); } return elements; @@ -804,8 +807,9 @@ Prado.WebUI.TBaseValidator.prototype = /** * @return boolean true if element is of checkbox or radio type. + * @private */ - isCheckBoxType : function(element) + _isCheckBoxType : function(element) { if(element && element.type) { @@ -817,8 +821,9 @@ Prado.WebUI.TBaseValidator.prototype = /** * @return boolean true if control to validate is of some of the TListControl type. + * @private */ - isListControlType : function() + _isListControlType : function() { var list = ['TCheckBoxList', 'TRadioButtonList', 'TListBox']; return list.include(this.options.ControlType); @@ -826,14 +831,15 @@ Prado.WebUI.TBaseValidator.prototype = /** * @return string gets the first selected list value, initial value if none found. + * @private */ - getFirstSelectedListValue : function() + _getFirstSelectedListValue : function() { var initial = ""; if(typeof(this.options.InitialValue) != "undefined") initial = this.options.InitialValue; - var elements = this.getListElements(); - var selection = this.getSelectedValuesAndChecks(elements, initial); + var elements = this._getListElements(); + var selection = this._getSelectedValuesAndChecks(elements, initial); return selection.values.length > 0 ? selection.values[0] : initial; } } @@ -862,7 +868,7 @@ Prado.WebUI.TRequiredFieldValidator = Class.extend(Prado.WebUI.TBaseValidator, else { var a = this.getValidationValue(); - var b = this.trim(this.options.InitialValue); + var b = this._trim(this.options.InitialValue); return(a != b); } } @@ -1099,13 +1105,13 @@ Prado.WebUI.TListControlValidator = Class.extend(Prado.WebUI.TBaseValidator, */ evaluateIsValid : function() { - var elements = this.getListElements(); + var elements = this._getListElements(); if(elements && elements.length <= 0) return true; this.observeListElements(elements); - var selection = this.getSelectedValuesAndChecks(elements); + var selection = this._getSelectedValuesAndChecks(elements); return this.isValidList(selection.checks, selection.values); }, @@ -1114,7 +1120,7 @@ Prado.WebUI.TListControlValidator = Class.extend(Prado.WebUI.TBaseValidator, */ observeListElements : function(elements) { - if(Prado.Browser().ie && this.isCheckBoxType(elements[0])) + if(Prado.Browser().ie && this._isCheckBoxType(elements[0])) { var validator = this; elements.each(function(element) @@ -1165,6 +1171,25 @@ Prado.WebUI.TListControlValidator = Class.extend(Prado.WebUI.TBaseValidator, } }); - - +/** + * TDataTypeValidator verifies if the input data is of the type specified + * by DataType option. + * The following data types are supported: + * - Integer A 32-bit signed integer data type. + * - Float A double-precision floating point number data type. + * - Date A date data type. + * - String A string data type. + * For Date type, the option DateFormat + * will be used to determine how to parse the date string. + */ +Prado.WebUI.TDataTypeValidator = Class.extend(Prado.WebUI.TBaseValidator, +{ + evaluateIsValid : function() + { + var value = this.getValidationValue(); + if(value.length <= 0) + return true; + return this.convert(this.options.DataType, value) != null; + } +}); diff --git a/framework/Web/UI/WebControls/TDataTypeValidator.php b/framework/Web/UI/WebControls/TDataTypeValidator.php index d78be7bf..ebcaace7 100644 --- a/framework/Web/UI/WebControls/TDataTypeValidator.php +++ b/framework/Web/UI/WebControls/TDataTypeValidator.php @@ -98,6 +98,19 @@ class TDataTypeValidator extends TBaseValidator return true; } + /** + * Returns an array of javascript validator options. + * @return array javascript validator options. + */ + protected function getClientScriptOptions() + { + $options = parent::getClientScriptOptions(); + $options['DataType']=$this->getDataType(); + if(($dateFormat=$this->getDateFormat())!=='') + $options['DateFormat']=$dateFormat; + return $options; + } + /** * This method overrides the parent's implementation. * The validation succeeds if the input data is of valid type. -- cgit v1.2.3